数据库
create database Test
go
use Test
go
create table UserInfo
(
id int primary key identity(1,1),
name varchar(20) not null unique,
pwd varchar(20) not null,
role int default(0) check(role=0 or role=1)
)
insert into UserInfo values('admin','admin',1)
insert into UserInfo values('user','123456',0)
DBherpel类
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class SQLherpel
{
//连接字符
private static string strConn = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
//查询
public static DataTable Query(string sql)
{
SqlDataAdapter adapter = new SqlDataAdapter(sql, strConn);
DataTable table = new DataTable();
adapter.Fill(table);
return table;
}
//非查询
public static int NonQuery(string sql)
{
int num = 0;
SqlConnection sqlConnection = new SqlConnection(strConn);
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
try
{
sqlConnection.Open();
num = sqlCommand.ExecuteNonQuery();
}
finally
{
if (sqlConnection.State == ConnectionState.Open)
sqlConnection.Close();
}
return num;
}
}
}
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="sql" connectionString="Data Source=.;Initial Catalog=Test;Integrated Security=True " providerName=""/>
</connectionStrings>
</configuration>
上面那个连接字符串写在web.config里面,例如
<connectionStrings>
<add name="随便名称,例如你的'[]'里面的字符串" connectionString="Data Source=.;Initial Catalog=你的数据库名称;Integrated Security=True;/>
</connectionStrings>
使用的时候要添加引用 “System.Configuration”,using System.Configuration;
第二个直接把连接字符串在代码中写死
登录
namespace BLL
{
public class UserInfoBLL
{
//登录
public static UserInfo Select(string name, string pwd)
{
return UserInfoDAL.Select(name, pwd);
}
}
}
namespace DAL
{
public class UserInfoDAL
{
public static UserInfo Select(string name,string pwd)
{
//构建查询语句 = 不能like
var sql = $"select * from UserInfo Where name='{name}' and pwd='{pwd}'";
var table = SQLherpel.Query(sql);
//如果没有数据 null
if(table !=null || table.Rows.Count < 1)
{
return null;
}
//如果有数据,取第一行,转换为对象
var row = table.Rows[0];
UserInfo user = new UserInfo()
{
Id = Convert.ToInt32(row["Id"]),
Name = Convert.ToString(row["Name"]),
Role = Convert.ToInt32(row["Role"]),
};
return user;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Web.Login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<dl>
<dd>账号:</dd>
<dt>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</dt>
</dl>
<dl>
<dd>密码:</dd>
<dt>
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox>
</dt>
</dl>
<dl>
<dd></dd>
<dt>
<asp:Button ID="btnLogin" runat="server" Text="登录" OnClick="btnLogin_Click" />
</dt>
</dl>
</form>
</body>
</html>
namespace Model
{
public class UserInfo
{
public int Id { get; set; }
public string Name { get; set; }
public string Pwd { get; set; }
public int Role { get; set; }
}
}