三层架构是哪三层
界面层(User Interface layer)
业务逻辑层(Business Logic Layer)
数据访问层(Data access layer)
如何搭建三层架构
步骤1:
文件---新建---项目---Windows窗体应用程序
名称:UIL
解决方案名称:Demo3Tier
选中解决方案名称:Demo3Tier
添加---新建项目---类库
名称:BLL
选中解决方案名称:Demo3Tier
添加---新建项目---类库
名称:DAL
选中解决方案名称:Demo3Tier
添加---新建项目---类库
名称:MODEL
完成之后如图所示
步骤2:
UIL添加引用
BLL,MODEL
BLL添加引用
DAL,MODEL
DAL添加引用
MODEL
这里只显示了UIL层的引用
步骤3:
MODEL编写F0092类文件
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace MODEL 7 { 8 public class F0092 9 { 10 string ulul01; 11 string ulul02; 12 string ulul03; 13 string ulul04; 14 string ulul05; 15 string ulul06; 16 string ulul07; 17 string ulul08; 18 string ulul09; 19 string ulul10; 20 string ulul11; 21 string ulul12; 22 string ulul13; 23 string ulul14; 24 string ulul15; 25 string ulul16; 26 string ulul17; 27 string ulul18; 28 string ulul19; 29 string ulul20; 30 string ulul21; 31 string ulul22; 32 string ulul23; 33 string ulul24; 34 string ulul25; 35 36 public string ULUL01 { get { return ulul01; } set { ulul01 = value; } } 37 public string ULUL02 { get { return ulul02; } set { ulul02 = value; } } 38 public string ULUL03 { get { return ulul03; } set { ulul03 = value; } } 39 public string ULUL04 { get { return ulul04; } set { ulul04 = value; } } 40 public string ULUL05 { get { return ulul05; } set { ulul05 = value; } } 41 public string ULUL06 { get { return ulul06; } set { ulul06 = value; } } 42 public string ULUL07 { get { return ulul07; } set { ulul07 = value; } } 43 public string ULUL08 { get { return ulul08; } set { ulul08 = value; } } 44 public string ULUL09 { get { return ulul09; } set { ulul09 = value; } } 45 public string ULUL10 { get { return ulul10; } set { ulul10 = value; } } 46 public string ULUL11 { get { return ulul11; } set { ulul11 = value; } } 47 public string ULUL12 { get { return ulul12; } set { ulul12 = value; } } 48 public string ULUL13 { get { return ulul13; } set { ulul13 = value; } } 49 public string ULUL14 { get { return ulul14; } set { ulul14 = value; } } 50 public string ULUL15 { get { return ulul15; } set { ulul15 = value; } } 51 public string ULUL16 { get { return ulul16; } set { ulul16 = value; } } 52 public string ULUL17 { get { return ulul17; } set { ulul17 = value; } } 53 public string ULUL18 { get { return ulul18; } set { ulul18 = value; } } 54 public string ULUL19 { get { return ulul19; } set { ulul19 = value; } } 55 public string ULUL20 { get { return ulul20; } set { ulul20 = value; } } 56 public string ULUL21 { get { return ulul21; } set { ulul21 = value; } } 57 public string ULUL22 { get { return ulul22; } set { ulul22 = value; } } 58 public string ULUL23 { get { return ulul23; } set { ulul23 = value; } } 59 public string ULUL24 { get { return ulul24; } set { ulul24 = value; } } 60 public string ULUL25 { get { return ulul25; } set { ulul25 = value; } } 61 } 62 }
步骤4:
UIL编写App.config配置文件
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <!--添加以下标签connectionStrings--> 4 <connectionStrings> 5 <add name="MSSQLDB" connectionString="server=.;uid=sa;pwd=123456;database=LemonTea;"/> 6 </connectionStrings> 7 8 </configuration>
步骤5:
DAL编写MssqlHelper类文件
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 //使用以下命名空间。 6 using System.Data; 7 using System.Data.SqlClient; 8 //需要添加引用 9 using System.Configuration; 10 11 namespace DAL 12 { 13 class MssqlHelper 14 { 15 //获取连接数据库的对象 16 public static SqlConnection GetConn() 17 { 18 //获取数据库连接字符串 19 string strAccess = ConfigurationManager.ConnectionStrings["MSSQLDB"].ConnectionString; 20 //得到连接数据库的对象 21 SqlConnection sqlConn = new SqlConnection(strAccess); 22 return sqlConn; 23 } 24 25 //查询的公共方法 26 public static DataSet SearchMethod(string strSQL, SqlParameter[] sqlPar) 27 { 28 //得到连接数据库的对象 29 SqlConnection sqlConn = GetConn(); 30 //得到执行SQL语句的对象 31 SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn); 32 //把参数添加到SqlCommand中。 33 if (sqlPar != null) 34 { 35 sqlCmd.Parameters.AddRange(sqlPar); 36 } 37 //适配器去执行 38 SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); 39 //得到数据的仓库 40 DataSet sqlDs = new DataSet(); 41 //填充数据 42 sqlDa.Fill(sqlDs); 43 return sqlDs; 44 } 45 46 //更新的公共方法 47 public static bool ModifyMethod(string strSQL, SqlParameter[] sqlPar) 48 { 49 bool result = false; 50 //得到连接数据库的对象 51 SqlConnection sqlConn = GetConn(); 52 //得到执行SQL语句的对象 53 SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn); 54 //把参数添加到SqlCommand中。 55 if (sqlPar != null) 56 { 57 sqlCmd.Parameters.AddRange(sqlPar); 58 } 59 60 try 61 { 62 //打开数据库连接 63 sqlConn.Open(); 64 //执行SQL语句 65 int numExec = sqlCmd.ExecuteNonQuery(); 66 if (numExec > 0) 67 { 68 result = true; 69 } 70 } 71 catch (Exception ex) 72 { 73 //抛出错误异常信息 74 throw ex; 75 } 76 finally 77 { 78 //关闭数据库连接 79 sqlConn.Close(); 80 } 81 return result; 82 } 83 84 } 85 }
DAL编写F0092类文件
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 //使用以下命名空间。 6 using System.Data; 7 using System.Data.SqlClient; 8 9 namespace DAL 10 { 11 public class F0092 12 { 13 //查询全部记录 14 public static List<MODEL.F0092> SelectAllRecord() 15 { 16 List<MODEL.F0092> list = new List<MODEL.F0092>(); 17 18 string strSQL = "select * from F0092"; 19 DataSet sqlDs = MssqlHelper.SearchMethod(strSQL, null); 20 21 foreach (DataRow dr in sqlDs.Tables[0].Rows) 22 { 23 MODEL.F0092 f0092 = new MODEL.F0092(); 24 f0092.ULUL01 = Convert.ToString(dr["ULUL01"]); 25 f0092.ULUL02 = Convert.ToString(dr["ULUL02"]); 26 f0092.ULUL03 = Convert.ToString(dr["ULUL03"]); 27 f0092.ULUL04 = Convert.ToString(dr["ULUL04"]); 28 f0092.ULUL05 = Convert.ToString(dr["ULUL05"]); 29 f0092.ULUL06 = Convert.ToString(dr["ULUL06"]); 30 f0092.ULUL07 = Convert.ToString(dr["ULUL07"]); 31 f0092.ULUL08 = Convert.ToString(dr["ULUL08"]); 32 f0092.ULUL09 = Convert.ToString(dr["ULUL09"]); 33 f0092.ULUL10 = Convert.ToString(dr["ULUL10"]); 34 f0092.ULUL11 = Convert.ToString(dr["ULUL11"]); 35 f0092.ULUL12 = Convert.ToString(dr["ULUL12"]); 36 f0092.ULUL13 = Convert.ToString(dr["ULUL13"]); 37 f0092.ULUL14 = Convert.ToString(dr["ULUL14"]); 38 f0092.ULUL15 = Convert.ToString(dr["ULUL15"]); 39 f0092.ULUL16 = Convert.ToString(dr["ULUL16"]); 40 f0092.ULUL17 = Convert.ToString(dr["ULUL17"]); 41 f0092.ULUL18 = Convert.ToString(dr["ULUL18"]); 42 f0092.ULUL19 = Convert.ToString(dr["ULUL19"]); 43 f0092.ULUL20 = Convert.ToString(dr["ULUL20"]); 44 f0092.ULUL21 = Convert.ToString(dr["ULUL21"]); 45 f0092.ULUL22 = Convert.ToString(dr["ULUL22"]); 46 f0092.ULUL23 = Convert.ToString(dr["ULUL23"]); 47 f0092.ULUL24 = Convert.ToString(dr["ULUL24"]); 48 f0092.ULUL25 = Convert.ToString(dr["ULUL25"]); 49 list.Add(f0092); 50 } 51 return list; 52 } 53 54 //查询单条记录 55 public static MODEL.F0092 SelectSingleRecord(string ulul01) 56 { 57 string strSQL = "select * from F0092 where ulul01 = @ulul01"; 58 SqlParameter[] sqlPar = { 59 new SqlParameter("@ulul01",ulul01) 60 }; 61 62 DataSet sqlDs = MssqlHelper.SearchMethod(strSQL, sqlPar); 63 64 MODEL.F0092 f0092 = new MODEL.F0092(); 65 f0092.ULUL01 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL01"]); 66 f0092.ULUL02 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL02"]); 67 f0092.ULUL03 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL03"]); 68 f0092.ULUL04 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL04"]); 69 f0092.ULUL05 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL05"]); 70 f0092.ULUL06 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL06"]); 71 f0092.ULUL07 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL07"]); 72 f0092.ULUL08 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL08"]); 73 f0092.ULUL09 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL09"]); 74 f0092.ULUL10 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL10"]); 75 f0092.ULUL11 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL11"]); 76 f0092.ULUL12 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL12"]); 77 f0092.ULUL13 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL13"]); 78 f0092.ULUL14 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL14"]); 79 f0092.ULUL15 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL15"]); 80 f0092.ULUL16 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL16"]); 81 f0092.ULUL17 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL17"]); 82 f0092.ULUL18 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL18"]); 83 f0092.ULUL19 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL19"]); 84 f0092.ULUL20 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL20"]); 85 f0092.ULUL21 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL21"]); 86 f0092.ULUL22 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL22"]); 87 f0092.ULUL23 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL23"]); 88 f0092.ULUL24 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL24"]); 89 f0092.ULUL25 = Convert.ToString(sqlDs.Tables[0].Rows[0]["ULUL25"]); 90 return f0092; 91 } 92 93 //添加记录 94 public static bool InsertRecord(MODEL.F0092 f0092) 95 { 96 string strSQL = "insert into F0092 values(@ulul01,@ulul02,@ulul03,@ulul04,@ulul05,@ulul06,@ulul07,@ulul08,@ulul09,@ulul10,@ulul11,@ulul12,@ulul13,@ulul14,@ulul15,@ulul16,@ulul17,@ulul18,@ulul19,@ulul20,@ulul21,@ulul22,@ulul23,@ulul24,@ulul25)"; 97 SqlParameter[] sqlPar = { 98 new SqlParameter("@ulul01",f0092.ULUL01), 99 new SqlParameter("@ulul02",f0092.ULUL02), 100 new SqlParameter("@ulul03",f0092.ULUL03), 101 new SqlParameter("@ulul04",f0092.ULUL04), 102 new SqlParameter("@ulul05",f0092.ULUL05), 103 new SqlParameter("@ulul06",f0092.ULUL06), 104 new SqlParameter("@ulul07",f0092.ULUL07), 105 new SqlParameter("@ulul08",f0092.ULUL08), 106 new SqlParameter("@ulul09",f0092.ULUL09), 107 new SqlParameter("@ulul10",f0092.ULUL10), 108 new SqlParameter("@ulul11",f0092.ULUL11), 109 new SqlParameter("@ulul12",f0092.ULUL12), 110 new SqlParameter("@ulul13",f0092.ULUL13), 111 new SqlParameter("@ulul14",f0092.ULUL14), 112 new SqlParameter("@ulul15",f0092.ULUL15), 113 new SqlParameter("@ulul16",f0092.ULUL16), 114 new SqlParameter("@ulul17",f0092.ULUL17), 115 new SqlParameter("@ulul18",f0092.ULUL18), 116 new SqlParameter("@ulul19",f0092.ULUL19), 117 new SqlParameter("@ulul20",f0092.ULUL20), 118 new SqlParameter("@ulul21",f0092.ULUL21), 119 new SqlParameter("@ulul22",f0092.ULUL22), 120 new SqlParameter("@ulul23",f0092.ULUL23), 121 new SqlParameter("@ulul24",f0092.ULUL24), 122 new SqlParameter("@ulul25",f0092.ULUL25) 123 }; 124 bool result = MssqlHelper.ModifyMethod(strSQL, sqlPar); 125 return result; 126 } 127 128 //删除记录 129 public static bool DeleteRecord(string ulul01) 130 { 131 string strSQL = "delete from F0092 where ulul01 = @ulul01"; 132 SqlParameter[] sqlPar = { 133 new SqlParameter("@ulul01",ulul01) 134 }; 135 bool result = MssqlHelper.ModifyMethod(strSQL, sqlPar); 136 return result; 137 } 138 139 //修改记录 140 public static bool UpdateRecord(MODEL.F0092 f0092) 141 { 142 string strSQL = "update F0092 set ulul02=@ulul02,ulul03=@ulul03,ulul04=@ulul04,ulul05=@ulul05,ulul06=@ulul06,ulul07=@ulul07,ulul08=@ulul08,ulul09=@ulul09,ulul10=@ulul10,ulul11=@ulul11,ulul12=@ulul12,ulul13=@ulul13,ulul14=@ulul14,ulul15=@ulul15,ulul16=@ulul16,ulul17=@ulul17,ulul18=@ulul18,ulul19=@ulul19,ulul20=@ulul20,ulul21=@ulul21,ulul22=@ulul22,ulul23=@ulul23,ulul24=@ulul24,ulul25=@ulul25 where ulul01=@ulul01"; 143 SqlParameter[] sqlPar = { 144 new SqlParameter("@ulul02",f0092.ULUL02), 145 new SqlParameter("@ulul03",f0092.ULUL03), 146 new SqlParameter("@ulul04",f0092.ULUL04), 147 new SqlParameter("@ulul05",f0092.ULUL05), 148 new SqlParameter("@ulul06",f0092.ULUL06), 149 new SqlParameter("@ulul07",f0092.ULUL07), 150 new SqlParameter("@ulul08",f0092.ULUL08), 151 new SqlParameter("@ulul09",f0092.ULUL09), 152 new SqlParameter("@ulul10",f0092.ULUL10), 153 new SqlParameter("@ulul11",f0092.ULUL11), 154 new SqlParameter("@ulul12",f0092.ULUL12), 155 new SqlParameter("@ulul13",f0092.ULUL13), 156 new SqlParameter("@ulul14",f0092.ULUL14), 157 new SqlParameter("@ulul15",f0092.ULUL15), 158 new SqlParameter("@ulul16",f0092.ULUL16), 159 new SqlParameter("@ulul17",f0092.ULUL17), 160 new SqlParameter("@ulul18",f0092.ULUL18), 161 new SqlParameter("@ulul19",f0092.ULUL19), 162 new SqlParameter("@ulul20",f0092.ULUL20), 163 new SqlParameter("@ulul21",f0092.ULUL21), 164 new SqlParameter("@ulul22",f0092.ULUL22), 165 new SqlParameter("@ulul23",f0092.ULUL23), 166 new SqlParameter("@ulul24",f0092.ULUL24), 167 new SqlParameter("@ulul25",f0092.ULUL25), 168 new SqlParameter("@ulul01",f0092.ULUL01) 169 }; 170 bool result = MssqlHelper.ModifyMethod(strSQL, sqlPar); 171 return result; 172 } 173 174 } 175 }
步骤6:
BLL编写F0092类文件
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 //使用以下命名空间。 6 using System.Data; 7 using System.Data.SqlClient; 8 9 namespace BLL 10 { 11 public class F0092 12 { 13 //查询全部记录 14 public static List<MODEL.F0092> SelectAllRecord() 15 { 16 return DAL.F0092.SelectAllRecord(); 17 } 18 19 //查询单条记录 20 public static MODEL.F0092 SelectSingleRecord(string ulul01) 21 { 22 return DAL.F0092.SelectSingleRecord(ulul01); 23 } 24 25 //添加记录 26 public static bool InsertRecord(MODEL.F0092 f0092) 27 { 28 return DAL.F0092.InsertRecord(f0092); 29 } 30 31 //删除记录 32 public static bool DeleteRecord(string ulul01) 33 { 34 return DAL.F0092.DeleteRecord(ulul01); 35 } 36 37 //修改记录 38 public static bool UpdateRecord(MODEL.F0092 f0092) 39 { 40 return DAL.F0092.UpdateRecord(f0092); 41 } 42 43 } 44 }
步骤7:
UIL编写代码。
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 UIL 11 { 12 public partial class MainForm : Form 13 { 14 public MainForm() 15 { 16 InitializeComponent(); 17 } 18 19 private void btnSearch_Click(object sender, EventArgs e) 20 { 21 dgvDisplay.DataSource = BLL.F0092.SelectAllRecord(); 22 } 23 } 24 }
完成步骤4~步骤7后如图所示
完成