最简单的三层架构实例.net版

时间:2021-11-09 05:23:06

          很多初学三层架构的用户,都对三层架构无从入手!而这些用户往往会通过搜索引擎搜索,例如“最简单的三层架构例子”,等关键词,就算用户找到这个实例,又会感觉不太明白,(心想有没有还可以再简单的例子)!

今天,我就写一个什么是最简单的三层架构例子(这个例子对你学习绝对有用,我说的!)

代码
         /// <summary>
         /// 初始化登录名称、登录密码(Model类)
         /// </summary>
         private string adminUser =string.Empty; //设置用户名称为空值
         private string adminPwd =string.Empty; //设置用户密码为空值
         public string AdminUser
         {
             get
             {
                return this.adminUser;
             }
             set
             {
                this.adminUser = value;
             }
         }
         public string AdminPwd
         {
             get
             {
                return this.adminPwd;
             }
             set
             {
                this.adminPwd = value;
             }
         }

代码
         /// <summary>
         /// 用户登录(BLL类)
         /// </summary>
         /// <paramname="m"></param>
         ///<returns></returns>
         public static intsysLogin(Model m)
         {
             stringstr = "adminValid"; //存储过程名称
            SqlParameter[] sqlParameter =
                {
                    //将UI层传递过来的用户名称和密码赋值给存储过程中的变量分别是adminUser和adminPwd(注意大小写)
                    new SqlParameter("adminUser",m.AdminUser),
                    new SqlParameter("adminPwd",m.AdminPwd)
                };
             DAL d= new DAL();
             returnInt32.Parse(d.ExecuteScalar(str,sqlParameter));
         }

代码
         /// <summary>
         /// 新建一个SQL登录链接
         /// </summary>
         ///<returns></returns>
         private static SqlConnectioncon()
         {
             returnnew SqlConnection("Data Source=localhost;Initial Catalog=数据库名称;Integrated Security=SSPI;");
         }
         /// <summary>
         /// 执行操作(DAL类)
         /// </summary>
         /// <paramname="str"></param>
         /// <paramname="sql"></param>
         ///<returns></returns>
         public stringExecuteScalar(string str, SqlParameter[] sql)
         {
             SqlConnectioncon = DAL.con();
             try
             {
                con.Open();
                SqlCommand com = new SqlCommand(str, con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddRange(sql);
                return Convert.ToString(com.ExecuteScalar()); //返回受影响的行数(例如影响的行数为1,那么返回数值1到BLL层,然后BLL层将数值1返回到UI层)
             }
             catch(Exception Error)
             {
                throw Error;
             }
            finally
             {
                con.Close();
             }
         }

代码
         //UI层
         Model m = new Model(); //实例化Model类
         m.AdminUser =this.TextBox1.Text.ToString(); //将文本框1中的值传递给Model类中的AdminUser
         m.AdminPwd =this.TextBox2.Text.ToString(); //将文本框2中的值传递给Model类中的AdminPwd
         if (BLL.sysLogin(m) > 0)
         {
            this.Label1.Text = "登录成功!马上进入管理平台...";
         }
         else
         {
            this.Label1.Text = "用户或密码错误,请重新输入!";
         }

代码
--存储过程(SQL2005)
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROC [dbo].[adminValid]
@adminUser varchar(20),@adminPwd varchar(120)
AS
SELECT COUNT(*) FROM admin WHERE adminUser = @adminUser AND adminPwd =@adminPwd