winform 三层(BLL.DAL.MODEL)

  图片

数据库设计相当简单,就一个数据库然后又张存着用户名和密码的表。
数据库设计:

数据库名称:threeLayer    

表: users     

列:1.username 账号   2.password  密码

数据库创建表的脚本如下:

[sql]  view plain  copy
  1. CREATE TABLE [dbo].[users](  
  2.     [id] [int] IDENTITY(1,1) NOT NULL,  
  3.     [username] [varchar](50) NULL,  
  4.     [password] [varchar](50) NULL,  
  5.  CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED   
  6. (  
  7.     [id] ASC  
  8. )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ONON [PRIMARY]  
  9. ON [PRIMARY]  
  10. GO  
  11. SET ANSI_PADDING OFF  
  12. GO  
  13. SET IDENTITY_INSERT [dbo].[users] ON  
  14. INSERT [dbo].[users] ([id], [username], [password]) VALUES (1, N'admin', N'admin')  
  15. INSERT [dbo].[users] ([id], [username], [password]) VALUES (2, N'user1', N'user1')  
  16. INSERT [dbo].[users] ([id], [username], [password]) VALUES (3, N'user2', N'user2')  
  17. INSERT [dbo].[users] ([id], [username], [password]) VALUES (4, N'user3', N'user3')  
  18. SET IDENTITY_INSERT [dbo].[users] OFF  


三层数据传递整体思路:

用户输入账号密码->点击登录->进入BLL层进行输入与数据的逻辑处理->进入DAL层将BAL层的逻辑进行实现(用户输入的账号的密码与数据库匹配),返回结果

其中数据的传递用model实体类属性来传递

步骤:

  1. 新建一个windows 窗体应用程序项目并命名为threeLayerText,路径自己选吧,可以选择回收站


  2. 在自动新建的窗体项目中,双击Form1.cs,打开窗体设计。


  3. 在窗体中添加两个label,两个TextBox分别命名为textBoxAccount、textBoxtextBoxPsw和一个button 命名为 butLogin。这里命名指的是控件name属性


  4. 为窗体添加一个应用配置文件:右键窗体项目文件-添加-新建项-应用程序配置文件

    1. 在配置文件<configuration>节点中添加数据库连接语句
    2. 根据数据库配置Initial Catalog 为数据库名称,User ID 为登录数据库账户,Password 为改账号密码
      添加后app.config完整内容如下
      [html]  view plain  copy
      1. <?xml version="1.0" encoding="utf-8" ?>  
      2. <configuration>  
      3.   <connectionStrings>  
      4.     <add name="dbConnection" connectionString="Data Source=.;Initial Catalog=threeLayer;Persist Security Info=True;User ID=sa;Password=123"  
      5.           providerName="SQLClient" />  
      6.   </connectionStrings>  
      7. </configuration>  

  5. 添加类库:右键项目解决方案-添加-新建项目-类库,命名,确定
    1. 分别添加DAL、BLL、Model三个类库

  6. 在Model类库中添加userInfo类,用于在各个层之间传递数据
    1. 在类库中新建一个用户类 userInfo:右键Model类库-添加-类(或者选中model类库,使用shift+alt+c快捷键)
    2. 在userInfo类中添加属性
      [csharp]  view plain  copy
      1. private string _username;  
      2. private string _psw;  
      3.   
      4. public string username  
      5. {  
      6.       set { _username = value; }  
      7.       get { return _username; }  
      8. }  
      9. public string psw  
      10. {  
      11.       set { _psw = value; }  
      12.       get { return _psw; }  
      13. }  
    3. Model类完整代码如下:
      [csharp]  view plain  copy
      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.Linq;  
      4. using System.Text;  
      5.   
      6. namespace Model  
      7. {  
      8.     public class userInfo  
      9.     {  
      10.         private string _username;  
      11.         private string _psw;  
      12.   
      13.         public string username  
      14.         {  
      15.             set { _username = value; }  
      16.             get { return _username; }  
      17.         }  
      18.         public string psw  
      19.         {  
      20.             set { _psw = value; }  
      21.             get { return _psw; }  
      22.         }  
      23.     }  
      24. }  

  7. 在DAL层中添加数据连接、查询操作类和方法
    1. 添加system.configuration引用,使类可以读取配置文件节点,读取配置文件中连接数据库语句;右键引用-添加引用-选择程序集-勾选-确定


    2. 添加 DBbase类(右键DAL项目-添加-新建项-命名好-确定) 用于连接数据库,添加System.Data 和 System.Data.SqlClient 命名空间,别问我用来干吗,其实我也不知道用来干吗的。
      创建一个基本的查询方法用于查询并返回记录条数。DBbase类完整代码如下:
      [csharp]  view plain  copy
      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.Linq;  
      4. using System.Text;  
      5. using System.Data;  
      6. using System.Data.SqlClient;  
      7.   
      8. namespace DAL  
      9. {  
      10.     public class DBbase  
      11.     {  
      12.         //读取配置文件 连接数据库语句  
      13.         public static string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;  
      14.         //public static string strCon = "Data Source=.;Initial Catalog=threeLayer;Persist Security Info=True;User ID=sa;Password=123";  
      15.           
      16.         //实例化连接对象 con  
      17.         SqlConnection con = new SqlConnection(strCon);  
      18.   
      19.         //检测连接是否打开  
      20.         public void chkConnection()  
      21.         {  
      22.             if (this.con.State == ConnectionState.Closed)  
      23.             {  
      24.                 this.con.Open();  
      25.             }  
      26.         }  
      27.   
      28.         //执行语句,返回该语句查询的数据行的总数  
      29.         public int returnRowCount(string strSQL)  
      30.         {  
      31.             chkConnection();  
      32.             try  
      33.             {  
      34.                 SqlDataAdapter da = new SqlDataAdapter(strSQL, con);  
      35.                 DataSet ds = new DataSet();  
      36.                 da.Fill(ds);  
      37.                 return ds.Tables[0].Rows.Count;  
      38.             }  
      39.             catch  
      40.             {  
      41.                 return 0;  
      42.             }  
      43.         }  
      44.     }  
      45. }  
    3. 添加 userAccess类(右键DAL项目-添加-新建项-命名好-确定) 用执行查询语句查找用户输入账号密码在数据库中存在记录条数
      userAccess类完整代码如下:
      [csharp]  view plain  copy
      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.Linq;  
      4. using System.Text;  
      5.   
      6. namespace DAL  
      7. {  
      8.     public class userAccess  
      9.     {  
      10.         //实例化DBbase 对象  
      11.         DBbase db = new DBbase();  
      12.   
      13.         //用户登录的方法  
      14.         public int userLogin(string name, string psw)  
      15.         {  
      16.             string strsql = "select * from users where username = '" + name + "' and password = '" + psw + "'";  
      17.             return db.returnRowCount(strsql);  
      18.         }  
      19.     }  
      20. }  


  8. 在BLL层中添加用户输入数据与数据库匹配的逻辑代码
    1. 添加Model、DAL类库引用:右键BLL类库中引用文件夹,右键-添加引用-选择解决方案-项目-选中Model、DAL-确定
    2. 实例化DAL.userAccess 类,并新建一个方法调用DAL.userAccess方法,参数为Model实体类中的useInfo类,完整代码如下:
      [csharp]  view plain  copy
      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.Linq;  
      4. using System.Text;   
      5.   
      6. namespace BLL  
      7. {  
      8.     public class userAccess  
      9.     {  
      10.         DAL.userAccess d_userAccess = new DAL.userAccess();  
      11.         public int userLogin(Model.userInfo m_userInfo)  
      12.         {  
      13.             return d_userAccess.userLogin(m_userInfo.username, m_userInfo.psw);  
      14.         }  
      15.     }  
      16. }  
  9. 回到窗体设计中,添加用户输入处理与调用BLL层方法
    1. 添加Model、DAL类库引用:右键threeLayerText项目中引用文件夹,右键-添加引用-选择解决方案-项目-选中Model、BLL-确定

    2. 打开窗体后台代码,实例化Model.userInfo 、BLL.userAccess。代码如下
      [csharp]  view plain  copy
      1. //实例化model层中 userInfo类用于传递数据  
      2. Model.userInfo m_userInfo = new Model.userInfo();  
      3.   
      4. //实例化BLL层中 userAccess方法衔接用户输入与数据库匹配  
      5. BLL.userAccess b_userAccess = new BLL.userAccess();  

    3. 双击登录按钮,添加点击事件。代码如下
      [csharp]  view plain  copy
      1. //将用户输入的账号密码 赋值给userInfo类 username、psw属性  
      2. m_userInfo.username = textBoxAccount.Text.Trim().ToString();  
      3. m_userInfo.psw = textBoxPsw.Text.Trim().ToString();  
      4.   
      5. //如果BLL层中 useLogin调用返回记录条数 大于1 则账号密码正确  
      6. if (b_userAccess.userLogin(m_userInfo) > 0)  
      7. {  
      8.    MessageBox.Show("登录成功");  
      9. }  
      10. else  
      11. {  
      12.    MessageBox.Show("登录失败");  
      13.  }  
    4. 完整Form1.cs 代码如下
      [csharp]  view plain  copy
      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.ComponentModel;  
      4. using System.Data;  
      5. using System.Drawing;  
      6. using System.Linq;  
      7. using System.Text;  
      8. using System.Windows.Forms;  
      9.   
      10. namespace threeLayerText  
      11. {  
      12.     public partial class Form1 : Form  
      13.     {  
      14.         //实例化model层中 userInfo类用于传递数据  
      15.         Model.userInfo m_userInfo = new Model.userInfo();  
      16.   
      17.         //实例化BLL层中 userAccess方法衔接用户输入与数据库匹配  
      18.         BLL.userAccess b_userAccess = new BLL.userAccess();  
      19.   
      20.         public Form1()  
      21.         {  
      22.             InitializeComponent();  
      23.         }  
      24.   
      25.         private void Form1_Load(object sender, EventArgs e)  
      26.         {  
      27.   
      28.         }  
      29.   
      30.         //登录按钮 事件  
      31.         private void butLogin_Click(object sender, EventArgs e)  
      32.         {  
      33.             //将用户输入的账号密码 赋值给userInfo类 username、psw属性  
      34.             m_userInfo.username = textBoxAccount.Text.Trim().ToString();  
      35.             m_userInfo.psw = textBoxPsw.Text.Trim().ToString();  
      36.   
      37.             //如果BLL层中 useLogin调用返回记录条数 大于1 则账号密码正确  
      38.             if (b_userAccess.userLogin(m_userInfo) > 0)  
      39.             {  
      40.                 MessageBox.Show("登录成功");  
      41.             }  
      42.             else  
      43.             {  
      44.                 MessageBox.Show("登录失败");  
      45.             }  
      46.         }  
      47.     }  
      48. }  

  10. 保存,可以调试了。


关于Model实体层的描述:
Model 又叫实体类,这个东西,大家可能觉得不好分层。我是这样理解的:UI<-->Model<-->BLL<-->Model<-->DAL,如此则认为Model在各层之间起到了一个数据传输的桥梁作用。不过在这里,我们不是把事情想简单,而是想复杂了。

猜你喜欢

转载自blog.csdn.net/adsl3373056/article/details/79468134
今日推荐