2023年6月21日发(作者:)

三层架构之基础篇(对数据库增删改查)在上⼀篇中已经搭建好了⼀个三层架构的框架,现在使⽤三层架构来对数据库进⾏增删改查操作:假设在数据库ItcastCater有⼀张表ManagerInfo,有如下⼏个字段

我们知道在UI,BLL,DAL之间有数据的交互,所以我们传递的数据需要有⼀个类型,因为我们操作的是ManagerInfo表,所以咱们可以在Model这⾥建⼀个类名为ManagerInfo的类,在这⾥定义的属性需要和ManagerInfo表中的字段⼀⼀对应。我们知道DAL是处理和数据库相关的操作,出了这个层就不在有和数据库相关的代码,所以我们需要封装⼀个SqlHelper类,⽤于数据库的操作。可以封装成⼀个静态类,作为⼯具类使⽤。同时,我们现在处理的是ManagerInfo这个表,以后也会操作其他的表,遵循单⼀原则,所以需要在DAL中建⼀个专门处理ManagerInfo表的类ManagerInfoDal,相应的在BLL中需要⼀个处理ManagerInfo表业务逻辑的类ManagerInfoBll。到这⾥所有的准备⼯作已经完成。(注意:我们使⽤的数据库是SQLite,所以在数据库操作那部分⽤的都是SQLite⾃带的 类和对象)1.查询操作:从最底层DAL开始写起(SQLiteHelper):

1 public static class SQLiteHelper 2 { 3 //连接字符串

4 static string strConn = tionStrings["Cater"].ConnectionString; 5 #region 查询数据 +DataTable GetList(string sql) 6 ///

7 /// 查询数据 8 /// 9 /// 查询字符串10 /// 11 public static DataTable GetList(string sql)12 {13 //创建连接对象14 using (SQLiteConnection conn = new SQLiteConnection(strConn))15 {16 //创建桥接对象17 SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, conn);18 //创建表 对象19 DataTable table = new DataTable();20 //将数据 缓存到 表格中21 (table);22 //返回数据23 return table;24 }25 }

26 #endregion27 }查询数据

接着是ManagerInfoDal类中查询代码 1 //由于出了DAL层就不允许有和数据库相关的代码,所以在这⾥返回的不是DataTable ,需要返回⼀个数据类型,在UI层接收时,使⽤⾯向对象来对数据进⾏操作 2 public List GetList() 3 { 4 DataTable table = t("select * from managerinfo"); 5 List list = new List(); 6 foreach (DataRow row in ) 7 { 8 //对象初始化器 9 (new ManagerInfo()10 {11 Mid = 32(row["mid"]),12 MName = row["mname"].ToString(),13 MPwd = row["mpwd"].ToString(),14 MType = 32(row["mtype"])15 });16 }17 return list;18

19 }

查询数据ManagerInfoBll中查询数据库的代码:

1 ///

2 /// 数据查询3 /// 4 /// 5 public List GetList()6 {7 return t();8 }查询数据BLL在前端调⽤的时候 只需要创建ManagerInfoBll对象,调⽤其查询数据⽅法即可。//创建ManagerInfoBll对象,以后的调⽤都是通过这个对象进⾏的ManagerInfoBll manager = new ManagerInfoBll();//取得了查询的数据List list = t();//咱们可以对这个数据集进⾏使⽤了。2.添加操作:UI层向BLL发送请求,即向数据库添加数据,那么需要向BLL传递需要添加数据的数据模型UI层代码://创建 ⼀个ManagerInfo对象ManagerInfo mi = new ManagerInfo(); = ng(); = ng(); = d ? 1 : 0;//将mi作为数据模型传给(mi)BLL层代码:

1 public bool Add(ManagerInfo mi)2 {3 return (mi);4 }添加数据BLLDAL层:⾸先会通过ManagerInfoDal层的代码,来调⽤SqlHelper中对数据库进⾏添加的代码ManagerInfoDal类:

1 ///

2 /// 插⼊数据 3 /// 4 /// 传⼊的数据模型 5 /// 6 public bool Insert(ManagerInfo mi) 7 { 8 string strSql = "insert into managerinfo (mname,mpwd,mtype) values(@mname,@mpwd,@mtype)"; 9 SQLiteParameter[] param = new SQLiteParameter[]

10 {

11 new SQLiteParameter("@mname", ),12 new SQLiteParameter("@mpwd",5()),13 new SQLiteParameter("@mtype",)

14 };15 return eNonQuery(strSql, param) > 0;16 }添加数据ManagerInfoDal在这⾥使⽤了SqlParameter 这个参数类,可以避免SQL注⼊SQLiteHelper类:

1 #region 向数据库插⼊数据,删除数据 修改数据 +ExecuteNonQuery(string sql) 2 ///

3 /// 向数据库插⼊数据,删除数据 修改数据 4 /// 5 /// 6 /// 7 public static int ExecuteNonQuery(string sql, params SQLiteParameter[] param) 8 { 9 using (SQLiteConnection conn = new SQLiteConnection(strConn))10 {11 SQLiteCommand cmd = new SQLiteCommand(sql, conn);12 ();13 ge(param);14 return eNonQuery();15 }16 }

17 #endregion添加数据 SQLiteHelper 3.删除操作:⼀般删除操作只需要根据主键进⾏删除,那么UI层向BLL层传递数据时,只需要传递ID即可。UI层:(Mid)BLL层:

1 public bool Remove(int id)2 {3 return ById(id);4 }删除数据 BLLDAL层:

1 ///

2 /// 根据id 删除数据 3 /// 4 /// 5 /// 6 public bool DeleteById(int id) 7 { 8 string strSql = "delete from managerinfo where mid=@id"; 9 SQLiteParameter[] param = new SQLiteParameter[]

10 {11 new SQLiteParameter("@id",id)12 };13 return eNonQuery(strSql, param) > 0;14 }根据id 删除数据 DALSQLiteHelper类中ExecuteNonQuery()⽅法 可以共⽤删除,添加,更新操作。4.更新操作:BLL层:

1 public bool Remove(int id)2 {3 return ById(id);4 }更新操作 BLLDAL层: 1 ///

2 /// 修改数据 3 /// 4 /// 5 /// 6 public bool Update(ManagerInfo mi) 7 { 8 List list = new List(); 9 string strSql = "update managerinfo set mname=@mname,";10 (new SQLiteParameter("@mname", ));11 if (!("******"))12 {13 strSql += "mpwd=@mpwd,";14 (new SQLiteParameter("@mpwd", ));15

16 }17 strSql += "mtype=@mtype where mid=@mid";18 (new SQLiteParameter("@mtype", ));19 (new SQLiteParameter("@mid", ));20 return eNonQuery(strSql, y()) > 0;21 }修改数据 DAL

在这⾥,额外说⼀下MD5加密:

1 public static string MD5(string str){ 2 //创建MD5对象 3 MD5 md5=(); 4 //将数据转换成字节数组 5 byte[] bytes=e(str);

6 // 对字节数组进⾏加密 7 byte[] bytes2= eHash(bytes); 8 //将字节数组转换成⼗六进制的字符串 9 //1.创建stringBuilder对象10 StringBuilder st = new StringBuilder();11 for (int i = 0; i < ; i++)12 {13 //将字节数组转换成16位14 Format(bytes2[i].ToString("x2"));15 }16 return ng();17 }MD5MD5加密 是不可逆的 ,所以有时在保存密码的时候需要MD5加密。

2023年6月21日发(作者:)

三层架构之基础篇(对数据库增删改查)在上⼀篇中已经搭建好了⼀个三层架构的框架,现在使⽤三层架构来对数据库进⾏增删改查操作:假设在数据库ItcastCater有⼀张表ManagerInfo,有如下⼏个字段

我们知道在UI,BLL,DAL之间有数据的交互,所以我们传递的数据需要有⼀个类型,因为我们操作的是ManagerInfo表,所以咱们可以在Model这⾥建⼀个类名为ManagerInfo的类,在这⾥定义的属性需要和ManagerInfo表中的字段⼀⼀对应。我们知道DAL是处理和数据库相关的操作,出了这个层就不在有和数据库相关的代码,所以我们需要封装⼀个SqlHelper类,⽤于数据库的操作。可以封装成⼀个静态类,作为⼯具类使⽤。同时,我们现在处理的是ManagerInfo这个表,以后也会操作其他的表,遵循单⼀原则,所以需要在DAL中建⼀个专门处理ManagerInfo表的类ManagerInfoDal,相应的在BLL中需要⼀个处理ManagerInfo表业务逻辑的类ManagerInfoBll。到这⾥所有的准备⼯作已经完成。(注意:我们使⽤的数据库是SQLite,所以在数据库操作那部分⽤的都是SQLite⾃带的 类和对象)1.查询操作:从最底层DAL开始写起(SQLiteHelper):

1 public static class SQLiteHelper 2 { 3 //连接字符串

4 static string strConn = tionStrings["Cater"].ConnectionString; 5 #region 查询数据 +DataTable GetList(string sql) 6 ///

7 /// 查询数据 8 /// 9 /// 查询字符串10 /// 11 public static DataTable GetList(string sql)12 {13 //创建连接对象14 using (SQLiteConnection conn = new SQLiteConnection(strConn))15 {16 //创建桥接对象17 SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, conn);18 //创建表 对象19 DataTable table = new DataTable();20 //将数据 缓存到 表格中21 (table);22 //返回数据23 return table;24 }25 }

26 #endregion27 }查询数据

接着是ManagerInfoDal类中查询代码 1 //由于出了DAL层就不允许有和数据库相关的代码,所以在这⾥返回的不是DataTable ,需要返回⼀个数据类型,在UI层接收时,使⽤⾯向对象来对数据进⾏操作 2 public List GetList() 3 { 4 DataTable table = t("select * from managerinfo"); 5 List list = new List(); 6 foreach (DataRow row in ) 7 { 8 //对象初始化器 9 (new ManagerInfo()10 {11 Mid = 32(row["mid"]),12 MName = row["mname"].ToString(),13 MPwd = row["mpwd"].ToString(),14 MType = 32(row["mtype"])15 });16 }17 return list;18

19 }

查询数据ManagerInfoBll中查询数据库的代码:

1 ///

2 /// 数据查询3 /// 4 /// 5 public List GetList()6 {7 return t();8 }查询数据BLL在前端调⽤的时候 只需要创建ManagerInfoBll对象,调⽤其查询数据⽅法即可。//创建ManagerInfoBll对象,以后的调⽤都是通过这个对象进⾏的ManagerInfoBll manager = new ManagerInfoBll();//取得了查询的数据List list = t();//咱们可以对这个数据集进⾏使⽤了。2.添加操作:UI层向BLL发送请求,即向数据库添加数据,那么需要向BLL传递需要添加数据的数据模型UI层代码://创建 ⼀个ManagerInfo对象ManagerInfo mi = new ManagerInfo(); = ng(); = ng(); = d ? 1 : 0;//将mi作为数据模型传给(mi)BLL层代码:

1 public bool Add(ManagerInfo mi)2 {3 return (mi);4 }添加数据BLLDAL层:⾸先会通过ManagerInfoDal层的代码,来调⽤SqlHelper中对数据库进⾏添加的代码ManagerInfoDal类:

1 ///

2 /// 插⼊数据 3 /// 4 /// 传⼊的数据模型 5 /// 6 public bool Insert(ManagerInfo mi) 7 { 8 string strSql = "insert into managerinfo (mname,mpwd,mtype) values(@mname,@mpwd,@mtype)"; 9 SQLiteParameter[] param = new SQLiteParameter[]

10 {

11 new SQLiteParameter("@mname", ),12 new SQLiteParameter("@mpwd",5()),13 new SQLiteParameter("@mtype",)

14 };15 return eNonQuery(strSql, param) > 0;16 }添加数据ManagerInfoDal在这⾥使⽤了SqlParameter 这个参数类,可以避免SQL注⼊SQLiteHelper类:

1 #region 向数据库插⼊数据,删除数据 修改数据 +ExecuteNonQuery(string sql) 2 ///

3 /// 向数据库插⼊数据,删除数据 修改数据 4 /// 5 /// 6 /// 7 public static int ExecuteNonQuery(string sql, params SQLiteParameter[] param) 8 { 9 using (SQLiteConnection conn = new SQLiteConnection(strConn))10 {11 SQLiteCommand cmd = new SQLiteCommand(sql, conn);12 ();13 ge(param);14 return eNonQuery();15 }16 }

17 #endregion添加数据 SQLiteHelper 3.删除操作:⼀般删除操作只需要根据主键进⾏删除,那么UI层向BLL层传递数据时,只需要传递ID即可。UI层:(Mid)BLL层:

1 public bool Remove(int id)2 {3 return ById(id);4 }删除数据 BLLDAL层:

1 ///

2 /// 根据id 删除数据 3 /// 4 /// 5 /// 6 public bool DeleteById(int id) 7 { 8 string strSql = "delete from managerinfo where mid=@id"; 9 SQLiteParameter[] param = new SQLiteParameter[]

10 {11 new SQLiteParameter("@id",id)12 };13 return eNonQuery(strSql, param) > 0;14 }根据id 删除数据 DALSQLiteHelper类中ExecuteNonQuery()⽅法 可以共⽤删除,添加,更新操作。4.更新操作:BLL层:

1 public bool Remove(int id)2 {3 return ById(id);4 }更新操作 BLLDAL层: 1 ///

2 /// 修改数据 3 /// 4 /// 5 /// 6 public bool Update(ManagerInfo mi) 7 { 8 List list = new List(); 9 string strSql = "update managerinfo set mname=@mname,";10 (new SQLiteParameter("@mname", ));11 if (!("******"))12 {13 strSql += "mpwd=@mpwd,";14 (new SQLiteParameter("@mpwd", ));15

16 }17 strSql += "mtype=@mtype where mid=@mid";18 (new SQLiteParameter("@mtype", ));19 (new SQLiteParameter("@mid", ));20 return eNonQuery(strSql, y()) > 0;21 }修改数据 DAL

在这⾥,额外说⼀下MD5加密:

1 public static string MD5(string str){ 2 //创建MD5对象 3 MD5 md5=(); 4 //将数据转换成字节数组 5 byte[] bytes=e(str);

6 // 对字节数组进⾏加密 7 byte[] bytes2= eHash(bytes); 8 //将字节数组转换成⼗六进制的字符串 9 //1.创建stringBuilder对象10 StringBuilder st = new StringBuilder();11 for (int i = 0; i < ; i++)12 {13 //将字节数组转换成16位14 Format(bytes2[i].ToString("x2"));15 }16 return ng();17 }MD5MD5加密 是不可逆的 ,所以有时在保存密码的时候需要MD5加密。