Use C # language binding framework for three simple database connection data registration function

Many people in the use of Microsoft Visual Studio do not know how to connect to the database and use the three-frame, and today we learn together how to use Microsoft Visual Studio to create a simple landing page.

工具:
Microsoft Visual Studio 2010
SQL Server Management Studio

Step:
1. First, create a new blank solution
Here Insert Picture Description
2. Right-click the solution we need to establish three.
Here Insert Picture Description
One thing to note in the establishment of three layers, because architecture is divided into three layers: presentation layer (UI (User Interface)), business logic layer (BLL (Business Logic Layer)) , Data Access Layer (DAL (Data Access Layer) ) plus physical library (Model) so we Model, DAL and the BLL and UI layers are built to establish libraries (Figure 1) is a web project (Figure 2)
Here Insert Picture Description
Figure I
Here Insert Picture Description
Figure II

The following table is to establish complete it later! One-third of it has been successful! Applaud it for yourself!
Here Insert Picture Description
How to link the three? Then there is the reference to it! Right-references, add references

Reference relationship:
the BLL and DAL need to reference the Model;
DAL need to reference the Model;
the Model entity, does not need to refer to other layers;
Here Insert Picture Description
next is connected to the database and execute the code operation friends -
establishing a table in a database, called the Test inside two and adding two data columns, as shown:
Here Insert Picture Description
Here Insert Picture Description
the final step, the codes.

Model层:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
    public class Class1
    {
        public string username {set;get;}
        public string userpwd { set; get; }
    }
}


Dal层:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Dal
{
    public class Class1
    {
        public List<Model.Class1> MyList(string where) 
        {
            DataSet ds = DB.ToGetData("select *from Test " + where);
            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                Model.Class1 MyClass1 = null;
                List<Model.Class1> MyList = new List<Model.Class1>();
                foreach (DataRow item in ds.Tables[0].Rows)
                {
                    MyClass1 = new Model.Class1();
                    MyClass1.username = item["username"].ToString();
                    MyClass1.userpwd = item["userpwd"].ToString();
                    MyList.Add(MyClass1);

                }
                return MyList;
            }
            else 
            {
                return null;
            }
        }
       
    }
    public class DB 
    {
        static string ConnStr = "Data Source=.;Initial Catalog=调用的数据库名;Persist Security Info=True;User ID=数据库用户名;Password=数据库密码";
       
        public static DataSet ToGetData(string Sql)
        {
            using (SqlConnection Conn = new SqlConnection(ConnStr))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(Sql, Conn))
                {
                    DataSet ds = new DataSet();
                    Conn.Open();
                    da.Fill(ds);
                    da.Dispose();
                    return ds;
                }
            }
        } 
    }
}


Bll层:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bll
{
    public class Class1
    {
        Dal.Class1 NewDal = new Dal.Class1();
        public List<Model.Class1> Login(string username, string userpwd)
        
        {
            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(userpwd))
            {
                return NewDal.MyList(" where username='"+username+"' and userpwd='"+userpwd+"'" );
            }
            else 
            {
                return null;
            }
        }
    }
}

UI层:(Login窗体)
1前端:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="UI.Login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       用户名:&nbsp <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
       用户密码: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /><br /><br />
       &nbsp&nbsp&nbsp <asp:Button ID="Button1" runat="server" Text="登Ì?陆?" 
            onclick="Button1_Click"  />
    </div>
    </form>
</body>
</html>


2.后端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UI
{
    public partial class Login : System.Web.UI.Page
    {
        Bll.Class1 NewBll = new Bll.Class1();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string username = TextBox1.Text.Trim();
            string userpwd = TextBox2.Text.Trim();
            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(userpwd))
            {
                List<Model.Class1> Myuserlist = NewBll.Login(username, userpwd);
                if (Myuserlist != null)
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "js", "<script>alert('登录成功!');</script>");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "js", "<script>alert('用户密或密码不存在!');</script>");
                }
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "js", "<script>alert('请输入所有信息!');</script>");
            }
        }
    }
}

In the UI layer click Run (F5), after the success of the experiment have input box.

Guess you like

Origin blog.csdn.net/weixin_44003632/article/details/86637817