2020-10-12

数据库   

 

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; }

    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值