这次通过反射技术来实现一下数据库表的增删改查对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping)
注:引用时约束了以下几点:
- 数据库表的表名与类的类名一致
- 数据库字段名和类字段名一致
注:如果用的是MySql数据库,有提供好的MySqlhelper非MySql数据库可以参考我写好的SqlHelper
SqlHelper参考位置:http://www.cnblogs.com/fengxuehuanlin/p/5271944.html
1.数据插入封装:
/// <summary> /// 完成数据的插入 /// 要求:1.数据库表名为类名 /// 2.数据库字段名和类字段名相同 /// </summary> /// <param name="obj">类的对象</param> public static void Insert(object obj) { StringBuilder Sql = new StringBuilder(); Type type = obj.GetType(); FieldInfo[] fields = type.GetFields();//获取类中所有字段信息,(表中的所有字段) string[] keys = new string[fields.Length-1];//存取字段信息 string[] param = new string[fields.Length-1];//存储参数化查询的参数 string[] values = new string[fields.Length-1];//每个字段对应的值 MySqlParameter[] sqlParameter = new MySqlParameter[fields.Length-1]; //参数化查询 string table = type.Name; //获取类名(表中的表名) int count = 0; foreach (FieldInfo field in fields) { if (field.Name != "id") //过滤掉自增字段id { keys[count] = field.Name; //字段名 try { param[count] = "@" + (field.GetValue(obj)).ToString(); //参数名 values[count] = (field.GetValue(obj)).ToString(); //字段值 MySqlParameter sqlparam = new MySqlParameter();//进行参数化查询 sqlparam.ParameterName = param[count]; //参数化查询用于替换的名字 sqlparam.Value = values[count]; //对应的值 sqlParameter[count] = sqlparam; count++; } catch(Exception e) { throw new Exception(keys[count]+"对象未初始化:"); //数据没有初始化时抛异常 } } } string sqlkey = string.Join(",", keys); //将字段数组转换为字符串 string sqlparamstr = string.Join(",", param); //将参数数组转换字符串 Sql.Append("insert into " + table + " (" + sqlkey + ") values (" + sqlparamstr + ")"); SqlHelper.ExecuteNonQuery(Sql.ToString(), sqlParameter); //通过SqlHelper完成操作 }
2.数据删除封装:
/// <summary> /// 更具id删除数据 /// </summary> /// <typeparam name="T">T为类名</typeparam> /// <param name="id">要删除的字段id</param> public static void Delete<T>(int id) { Type type = typeof(T); string table = type.Name;//获取表名 StringBuilder Sql = new StringBuilder(); Sql.Append("delete from "+table+" where id=@id"); if (SqlHelper.ExecuteNonQuery(Sql.ToString(), new MySqlParameter("@id", id)) <= 0) { throw new Exception("这条记录不存在"); } }
3.数据更新封装:
/// <summary> /// 更新一个字段 /// </summary> /// <typeparam name="T">T为类名(也就是数据库表)</typeparam> /// <param name="id">更新哪条数据</param> /// <param name="name">更新哪个字段</param> /// <param name="value">字段对应的值</param> public static void Update<T>(int id,string name,object value) { Type type = typeof(T); string table = type.Name;//获取表名 StringBuilder Sql = new StringBuilder(); Sql.Append("Update " + table + " set "+name+"=@value where id=@id"); MySqlParameter[] param = { new MySqlParameter("@value", value), new MySqlParameter("@id", id) }; if(SqlHelper.ExecuteNonQuery(Sql.ToString(), param)<=0) { throw new Exception("这条记录不存在"); } }
4.数据查询封装:
/// <summary> /// 根据id查询值 /// </summary> /// <typeparam name="T">T为类名(也就是数据库表)</typeparam> /// <param name="id"></param> /// <returns></returns> public static object Select<T>(int id)where T:new() { Type type = typeof(T); string table = type.Name;//获取表名 StringBuilder Sql = new StringBuilder(); Sql.Append("select *from "+table+" where id=@id"); DataTable tab = SqlHelper.ExecuteQuery(Sql.ToString(), new MySqlParameter("@id", id)); if (tab.Rows.Count <= 0) { //没有查询到数据 return default(T); } else if (tab.Rows.Count > 1) { throw new Exception("查询到了多条数据"); } DataRow row =tab.Rows[0]; T obj = new T(); FieldInfo[] fields= type.GetFields(); foreach (FieldInfo field in fields) { string Name = field.Name; //获取每一个字段名 object value = row[Name]; //将数据库中读取到的每一个值进行赋值 field.SetValue(obj, value); } return obj; }