(超简单基础版)ASP.NET(.Net Framework)三层架构,结合sql server实现登录与注册

Posted Growing Hacker

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(超简单基础版)ASP.NET(.Net Framework)三层架构,结合sql server实现登录与注册相关的知识,希望对你有一定的参考价值。

第一步:首先创建一个Models类库(解决方案上)

1.使用面向对象的方法封装一个UserInfo类

 public class UserInfo
   
        public UserInfo()
       

        public UserInfo(int id, string uid, string pwd, string name, int age, string gender)
       
            this.id = id;
            this.uid = uid;
            this.pwd = pwd;
            this.name = name;
            this.age = age;
            this.gender = gender;
       
        public UserInfo( string uid, string pwd, string name, int age, string gender)
       
            
            this.uid = uid;
            this.pwd = pwd;
            this.name = name;
            this.age = age;
            this.gender = gender;
       

        public int id get; set;
        public string uid get; set;
        public string pwd get; set;
        public string name get; set;
        public int age get; set;
        public string gender get; set;
   

第二步:创建我们的 “打工仔” DAL 类库,将它与我们的Models层关联(展开选项,右击引用,添加引用)(解决方案上)

1.可爱的DBHelper登场(超超超简单版~)

 public class DBHelper
   

        //连接对象
        public static SqlConnection con = new SqlConnection("server=.;database=DB2003;uid=sa;pwd=123");
        //判断连接是否打开
        public static void OpenOrClose()
       
            if (con.State == ConnectionState.Open)
           
                con.Close();
           
            if (con.State == ConnectionState.Closed)
           
                con.Open();
           
       
        //增删改
        public static bool Execute(string sql)
       
            OpenOrClose();
            SqlCommand cmd = new SqlCommand(sql, con);
            int count = cmd.ExecuteNonQuery();//增删改方法
            OpenOrClose();
            return count > 0;
       
        //非断开式查询
        public static SqlDataReader GetReader(string sql)
       
            OpenOrClose();
            SqlCommand cmd = new SqlCommand(sql, con);
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
       
        //断开式查询
        public static DataTable GetTable(string sql)
       
            OpenOrClose();
            SqlDataAdapter sda = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            OpenOrClose();
            return dt;
       
   

2.创建UserInfoDAL类

public  class UserInfoDAL
   
        //使用非断开式查询
        public SqlDataReader GetReader(string uid, string pwd)
       
            string sql = string.Format("SELECT * FROM user_info where uid='0' and pwd='1'", uid, pwd);
            return DBHelper.GetReader(sql);
       
        //新增用户信息
        public bool AddUser(UserInfo user)
       
            string sql = string.Format("insert into user_info values('0','1','2','3','4')"
                ,user.uid,user.pwd,user.name,user.age,user.gender);
            return DBHelper.Execute(sql);

       
   

第三步:创建我们的 “CEO”   BLL类库(解决方案上),将它与我们的Models、DAL层关联

1.同样,创建一个UserInfoBLL类

 public class UserInfoBLL
   
        //调用UserInfoDAL的方法
        UserInfoDAL dal = new UserInfoDAL();

        public UserInfo Login(string uid, string pwd)
       
         SqlDataReader reader=   dal.GetReader(uid ,pwd);
            UserInfo user = null;
            //判断SqlDataReader中是否有数据
            if (reader.Read())
           
                user = new UserInfo();
                user.id = reader.GetInt32(0);
                user.uid = reader.GetString(1);
                user.pwd = reader.GetString(2);
                user.name = reader.GetString(3);
                user.age = reader.GetInt32(4);
                user.gender = reader.GetString(5);
           
            DBHelper.OpenOrClose();
            return user;
       
        //用户注册,
        public bool reg(string uid, string pwd, string name, int age, string gender)
       
            return dal.AddUser(new UserInfo(uid, pwd, name, age, gender));
       
   

第四步:创建web窗体(项目上)将它与我们的Models、BLL层关联

1.1.创建登录页面

<body>
    <form id="form1" runat="server">
        <div>
            <p>账号:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </p>
            <p>密码:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </p>
            <p>
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="登录" />
                <asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/Reg.aspx">注册</asp:LinkButton>
            </p>
        </div>
    </form>
</body>

1.2.登录页面后台代码

public partial class Login : System.Web.UI.Page
   
        protected void Page_Load(object sender, EventArgs e)
       

       

        protected void Button1_Click(object sender, EventArgs e)
       
            string uid = this.TextBox1.Text;
            string pwd = this.TextBox2.Text;

            UserInfoBLL bll = new UserInfoBLL();
            //调用BLL层的Login方法,返回用户对象
          UserInfo user=  bll.Login(uid,pwd);
            //根据用户对象是否为空,来判断登录操作是否成功
            if (user != null)
           
                Session["user"] = user;
                Response.Redirect("Index.aspx");
           
            else
           
                Response.Write("<script>alert('登录失败!')</script>");
           
       
   

2.1.创建注册页面

<body>
    <form id="form1" runat="server">
        <div>
            <h2>用户注册</h2>
            <p>账号:<asp:TextBox ID="txtuid" runat="server"></asp:TextBox>
            </p>
            <p>密码:<asp:TextBox ID="txtpwd" runat="server"></asp:TextBox>
            </p>
            <p>姓名:<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
            </p>
            <p>年龄:<asp:TextBox ID="txtage" runat="server"></asp:TextBox>
            </p>
            <p>性别:<asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>男</asp:ListItem>
                <asp:ListItem>女</asp:ListItem>
                </asp:DropDownList>
            </p>
            <p>
                <asp:Button ID="Button1" runat="server" Text="注册" OnClick="Button1_Click" style="height: 21px" />
            </p>
        </div>
    </form>
</body>

2.2.注册页面后台代码

 public partial class Reg : System.Web.UI.Page
   
        protected void Page_Load(object sender, EventArgs e)
       

       

        protected void Button1_Click(object sender, EventArgs e)
       
            UserInfoBLL bll = new UserInfoBLL();
            if (bll.reg(txtuid.Text, txtpwd.Text, txtname.Text, int.Parse(txtage.Text), DropDownList1.Text))
           
                Response.Write("<script>alert('注册成功')</script>");
           
            else
           
                Response.Write("<script>alert('注册失败!')</script>");
           
       
   

OK!大功告成!最后提示一下,如果要引用其他层的东西,记得alt + enter 一下,选择using 那个选项。

Jquery AJAX ASP.NET IIS 跨域 超简单解决办法

第一种:

在IIS添加如下标头即可

Access-Control-Allow-Headers:Content-Type, api_key, Authorization
Access-Control-Allow-Origin:*

 

技术分享

完美解决跨域!

 

第二种:

技术分享

技术分享

调用端的项目也要同上面设置一样

 

$.ajax({
  url: "http://172.16.2.22:1212/api/values/3",
  type: "GET",
  dataType: "json",
  success: function (data) {
    console.log(data);
  },
  error: function (msg) {
    debugger
  }
})

 

 

技术分享
技术分享

 

技术分享
技术分享

以上是关于(超简单基础版)ASP.NET(.Net Framework)三层架构,结合sql server实现登录与注册的主要内容,如果未能解决你的问题,请参考以下文章

十四个关于ASP.NET基础知识问答(C#版)

基础版限时免费致敬WebForms,ASP.NET Core也能这么玩!

ASP.NET Core 配置 MVC - ASP.NET Core 基础教程 - 简单教程,简单编程

ASP.NET Core 动作结果 - ASP.NET Core 基础教程 - 简单教程,简单编程

一步步完成“迷你版” 的ASP.NET Core框架

会话超时时创建一个新的 ASP.NET_SessionId