黑马程序员----三层架构(没有另建一个BLL业务层--类库)

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

分为UI(表现)层DAL(数据访问)层(BLL层包含在其中)Model,在添加项目时;如分别添加Winfrom(WPF、控制台、ASP.NET等应用程序,生成的是.exe文件),而DAL和Model添加的是类库,他们生成后都是程序集(.DLL文件),这两个类库都是用来放类的,DAL层放的是有关SQL语句的类,用来访问数据库的,如SQLHelp;而Model是用来放一般类的;原则上"UI层不能包含SQL语句",当然特殊情况下还是可以在UI层写SQL语句的,如多条件的搜索(拼接SQL的字符串如where @name=name and @age=age.....)

例如.....

代码:

DAL层的SQLHelp类和Common类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace HRMSYSDAL
{
    class SqlHelp
    {
        //获取连接字符串
        private static string connectionStr = ConfigurationManager.ConnectionStrings["dbTable"].ConnectionString;

        //sql是SQL语句,及插入可变长度参数数组parameter[]  注意类型要一致
        public static int ExecuteNonQuery(string sql,params SqlParameter[] param)
        {
            using (SqlConnection con = new SqlConnection(connectionStr))
            {
                con.Open();
                using (SqlCommand command = new SqlCommand(sql, con))
                {
                    //将可变参数数组变量添加到集合里
                    foreach (SqlParameter p in param)
                    {
                        command.Parameters.Add(p);
                    }
                    int num = command.ExecuteNonQuery();
                    return num;
                }
            }
        }

        //返回一个要查询的表
        public static DataTable GetQuery(string sql,params SqlParameter[] param)
        {
            using (SqlConnection con = new SqlConnection(connectionStr))
            {
                using (SqlCommand command = new SqlCommand(sql, con))
                {
                    command.Parameters.AddRange(param);  //和上面foreach遍历一样,添加参数到集合里
                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    DataTable table = new DataTable();
                    adapter.Fill(table);
                    return table;
                }
            }
        }

        //返回第一行的第一列数据
        public static object ExecuteScalar(string sql, params SqlParameter[] param)
        {
            using (SqlConnection con = new SqlConnection(connectionStr))
            {
                con.Open();
                using (SqlCommand command = new SqlCommand(sql, con))
                {
                    foreach (SqlParameter p in param)
                    {
                        command.Parameters.Add(p);
                    }
                    return command.ExecuteScalar();
                }
            }
        }
    }
}

Common类: (应该算是BLL业务层)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HRMSYSModel;          //注意DAL层要引用Model层的类,因为我写在DAL层,所以不需要引用DAL层
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.Collections;

namespace HRMSYSDAL
{
    public class Common
    {
        //注册管理员信息,向数据库提交信息
        public static int PostInfo(Operater p)
        {
            int num = SqlHelp.ExecuteNonQuery(@"insert into T_Operater(operater,password,isDelete)
            values(@operater,@password,@isDelete)", new SqlParameter("@operater", p.OperaterName),
            new SqlParameter("@password", p.Password),new SqlParameter("@isDelete",p.IsDelete)); //默认插入false,代表默认"软删除"没删除管理员
            return num;
        }

}

 

Model类库的一个Operater类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HRMSYSModel
{
    public class Operater
    {
        public string OperaterName { set; get; }
        public string Password { set; get; }
        public bool IsDelete { set; get; }
        public Guid ID { set; get; }
    }
}

 

UI层(这里用的是winfrom应用程序)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRMSYSDAL;     //注意UI层要引用DAL层和Model层
using HRMSYSModel;
using HRMSYSUI.WinFromSurface;

namespace HRMSYSUI
{

    public partial class LogIn : Form
    {
        public LogIn()
        {
            InitializeComponent();
        }

        MainShow MS = new MainShow();

        int errorTime = 0;
        private void btnLogIn_Click(object sender, EventArgs e)
        {
            string name = txtLogInUserName.Text.Trim();
            string password = txtLogInPassword.Text.Trim();
            //验证文本框是否为空
            if (Common.CheckTextBox(txtLogInUserName, txtLogInPassword))
            {
                return;
            }
            try
            {
                Common.GetOperater(name);  //要是获取不到数据库中用户名,则抛出异常
            }
            catch
            {
                MessageBox.Show("用户名不存在!");
                return;
            }
            if(errorTime>=3)
            {
                MessageBox.Show("错误次数过多,要锁定输入的文本框!");
                txtLogInUserName.Enabled = false;
                txtLogInPassword.Enabled = false;
                return;
            }
            if (Common.GetPassword(name) == MD5.GetMD5((password + MD5.GetSalt()))&&errorTime<3)
            {
                MessageBox.Show("登陆成功!");
                errorTime = 0;
                this.Hide();
                MS.Show();
               
            }
            else
            {
                MessageBox.Show("用户名或密码有误!");
                errorTime++;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
           
        }

        //加载表数据
        public void DGShowInfo()
        {
            MS.dgShow.DataSource = Common.GetPersonInfo();
        }
    }
}

 

而我写的应该也是三层,Common类不就是引用DAL层的SQLHelp类和Model类库嘛,其实它就是BLL业务层!

原则上:三层是-----DAL数据层引用Model类库     BLL业务层引用DAL层和Model类库      UI表现层引用BLL层和Model类库(三层我是这么理解的!望纠正!!)

(注意:以上的代码只是为了举例复制了一部分,不是很完整)

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值