在上一篇中已经搭建好了一个三层架构的框架,现在使用三层架构来对数据库进行增删改查操作:
假设在数据库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 = ConfigurationManager.ConnectionStrings["Cater"].ConnectionString; 5 #region 查询数据 +DataTable GetList(string sql) 6 /// <summary> 7 /// 查询数据 8 /// </summary> 9 /// <param name="sql">查询字符串</param> 10 /// <returns></returns> 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 adapter.Fill(table); 22 //返回数据 23 return table; 24 } 25 } 26 #endregion 27 }
接着是ManagerInfoDal类中查询代码

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

1 /// <summary> 2 /// 数据查询 3 /// </summary> 4 /// <returns></returns> 5 public List<ManagerInfo> GetList() 6 { 7 return miDal.GetList(); 8 }
在前端调用的时候 只需要创建ManagerInfoBll对象,调用其查询数据方法即可。
//创建ManagerInfoBll对象,以后的调用都是通过这个对象进行的
ManagerInfoBll manager = new ManagerInfoBll();
//取得了查询的数据
List<ManagerInfo> list = manager.GetList();
//咱们可以对这个数据集进行使用了。
2.添加操作:
UI层向BLL发送请求,即向数据库添加数据,那么需要向BLL传递需要添加数据的数据模型
UI层代码:
//创建 一个ManagerInfo对象
ManagerInfo mi = new ManagerInfo();
mi.MName = txtName.Text.ToString();
mi.MPwd = txtPwd.Text.ToString();
mi.MType = rb1.Checked ? 1 : 0;
//将mi作为数据模型传给BLL
manager.Add(mi)
BLL层代码:

1 public bool Add(ManagerInfo mi) 2 { 3 return miDal.Insert(mi); 4 }
DAL层:首先会通过ManagerInfoDal层的代码,来调用SqlHelper中对数据库进行添加的代码
ManagerInfoDal类:

1 /// <summary> 2 /// 插入数据 3 /// </summary> 4 /// <param name="mi">传入的数据模型</param> 5 /// <returns></returns> 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", mi.MName), 12 new SQLiteParameter("@mpwd",MD5Helper.Md5(mi.MPwd)), 13 new SQLiteParameter("@mtype",mi.MType) 14 }; 15 return SQLiteHelper.ExecuteNonQuery(strSql, param) > 0; 16 }
在这里使用了SqlParameter 这个参数类,可以避免SQL注入
SQLiteHelper类:

1 #region 向数据库插入数据,删除数据 修改数据 +ExecuteNonQuery(string sql) 2 /// <summary> 3 /// 向数据库插入数据,删除数据 修改数据 4 /// </summary> 5 /// <param name="sql"></param> 6 /// <returns></returns> 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 conn.Open(); 13 cmd.Parameters.AddRange(param); 14 return cmd.ExecuteNonQuery(); 15 } 16 } 17 #endregion
3.删除操作:
一般删除操作只需要根据主键进行删除,那么UI层向BLL层传递数据时,只需要传递ID即可。
UI层:
manager.Remove(Mid)
BLL层:

1 public bool Remove(int id) 2 { 3 return miDal.DeleteById(id); 4 }
DAL层:

1 /// <summary> 2 /// 根据id 删除数据 3 /// </summary> 4 /// <param name="id"></param> 5 /// <returns></returns> 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 SQLiteHelper.ExecuteNonQuery(strSql, param) > 0; 14 }
SQLiteHelper类中ExecuteNonQuery()方法 可以共用删除,添加,更新操作。
4.更新操作:
BLL层:

1 public bool Remove(int id) 2 { 3 return miDal.DeleteById(id); 4 }
DAL层:

1 /// <summary> 2 /// 修改数据 3 /// </summary> 4 /// <param name="mi"></param> 5 /// <returns></returns> 6 public bool Update(ManagerInfo mi) 7 { 8 List<SQLiteParameter> list = new List<SQLiteParameter>(); 9 string strSql = "update managerinfo set mname=@mname,"; 10 list.Add(new SQLiteParameter("@mname", mi.MName)); 11 if (!mi.MPwd.Equals("******")) 12 { 13 strSql += "mpwd=@mpwd,"; 14 list.Add(new SQLiteParameter("@mpwd", mi.MPwd)); 15 16 } 17 strSql += "mtype=@mtype where mid=@mid"; 18 list.Add(new SQLiteParameter("@mtype", mi.MType)); 19 list.Add(new SQLiteParameter("@mid", mi.Mid)); 20 return SQLiteHelper.ExecuteNonQuery(strSql, list.ToArray()) > 0; 21 }
在这里,额外说一下MD5加密:

1 public static string MD5(string str){ 2 //创建MD5对象 3 MD5 md5=MD5.Create(); 4 //将数据转换成字节数组 5 byte[] bytes=Encoding.utf8.GetByte(str); 6 // 对字节数组进行加密 7 byte[] bytes2= md5.ComputeHash(bytes); 8 //将字节数组转换成十六进制的字符串 9 //1.创建stringBuilder对象 10 StringBuilder st = new StringBuilder(); 11 for (int i = 0; i < bytes2.Length; i++) 12 { 13 //将字节数组转换成16位 14 st.AppendFormat(bytes2[i].ToString("x2")); 15 } 16 return st.ToString(); 17 }
MD5加密 是不可逆的 ,所以有时在保存密码的时候需要MD5加密。