一个简单的三层实现

Model代码

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

            }

        }

BLL层:

         /// <summary>

        /// 用户登录(BLL类)

        /// </summary>

        /// <param name="m"></param>

        /// <returns></returns>

        public static int sysLogin(Model m)

        {

            string str = "adminValid"; //存储过程名称

            SqlParameter[] sqlParameter =

                {

                    //将UI层传递过来的用户名称和密码赋值给存储过程中的变量分别是adminUser和adminPwd(注意大小写)

                    new SqlParameter("adminUser",m.AdminUser),

                    new SqlParameter("adminPwd",m.AdminPwd)

                };

            DAL d = new DAL();

            return Int32.Parse(d.ExecuteScalar(str,sqlParameter));

        }



DAL层:

        /// <summary>

        /// 新建一个SQL登录链接

        /// </summary>

        /// <returns></returns>

        private static SqlConnection con()

        {

            return new SqlConnection("Data Source=localhost;Initial Catalog=数据库名称;Integrated Security=SSPI;");

        }

        /// <summary>

        /// 执行操作(DAL类)

        /// </summary>

        /// <param name="str"></param>

        /// <param name="sql"></param>

        /// <returns></returns>

        public string ExecuteScalar(string str, SqlParameter[] sql)

        {

            SqlConnection con = 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层:

         //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

你可能感兴趣的:(实现)