时间:2021-07-01 10:21:17 帮助过:6人阅读
二、Edit
描述:对字段进行修改。
设计思路:传入修改之前和修改之后的俩个实体类,对这俩个实体类行进比较,对于俩者之间的不同。记录下来,然后进行数据库的Update操作。
方法名: public int Edit(object obj,object newObj,string DJID,string DJIDName)
参数:obj:修改之前的实体类。
newObj:修改之后的实体类。
DJIDName:主键ID, 用来匹配Where条件。
DJID:主键ID值,因为ID的唯一性
#region Edit /// <summary> /// 修改 /// </summary> /// <param name="obj">之前的数据</param> /// <param name="newObj">修改之后的数据</param> /// <param name="DJID">主键</param> /// <returns>结果</returns> public int Edit(object obj,object newObj,string DJID,string DJIDName) { Type objType = obj.GetType(); string tableName = objType.Name; Type newObjType = newObj.GetType(); PropertyInfo[] objInfos = objType.GetProperties(); PropertyInfo[] newObjInfos = newObjType.GetProperties(); StringBuilder sb = new StringBuilder(); sb.AppendFormat("update {0} set", tableName); foreach (PropertyInfo objInfo in objInfos) { foreach (PropertyInfo newObjInfo in newObjInfos) { if (objInfo.Name == newObjInfo.Name) { object value = objInfo.GetValue(obj, null); object newValue = newObjInfo.GetValue(newObj, null); if (!value.Equals(newValue)&& newValue != null) { if (objInfo.PropertyType.Name.ToLower() == "datetime") { DateTime dt = (DateTime)newValue; string time = dt.ToString("yyyy-MM-dd HH:mm:ss"); if (time.Contains("0001-01-01")) { sb.AppendFormat(" {0}=‘{1}‘,", newObjInfo.Name, DBNull.Value); } else { sb.AppendFormat(" {0}=‘{1}‘,", newObjInfo.Name, time); } } if (objInfo.PropertyType.Name.ToLower() == "string") { sb.AppendFormat(" {0}=‘{1}‘,", newObjInfo.Name, Convert.ToString(newValue)); } if (objInfo.PropertyType.Name.ToLower() == "boolean") { ; char flag = ‘0‘; if (Convert.ToBoolean(newValue) == true) { flag = ‘1‘; } else { flag = ‘0‘; } sb.AppendFormat(" {0}={1},", newObjInfo.Name, flag); } if (objInfo.PropertyType.Name.ToLower() == "int32") { sb.AppendFormat(" {0}={1},", newObjInfo.Name, Convert.ToInt32(newValue)); } if (objInfo.PropertyType.Name.ToLower() == "single") { sb.AppendFormat(" {0}={1},", newObjInfo.Name, Convert.ToDouble(newValue)); } if (objInfo.PropertyType.Name.ToLower() == "decimal") { sb.AppendFormat(" {0}={1},", newObjInfo.Name, Convert.ToDecimal(newValue)); } } break; } } } string sqlHA = sb.ToString(); string SQL=sqlHA.Remove(sqlHA.Length - 1); SQL += " where " + DJIDName + "=" + "‘" + DJID + "‘"; int count=SaveOrUpdate(SQL); return count; } #endregionView Code
三、Select
描述:查询数据库中表的所有信息,并将表中的记录保存到实体类中,实体类放到ArrayList中。
设计思路:使用泛型和反射。通过利用泛型来获取实体类的类型,利用反射来获取实体类字段,并进行赋值。
方法名: public ArrayList Select<T>(string tableName, string sql = "") where T : new().。声明了一个泛型方法。
参数:tableName:数据库中想要查询的数据表。
sql=“”;查询的SQL
#region Select public ArrayList Select<T>(string tableName, string sql = "") where T : new() { ArrayList list = new ArrayList(); DataTable dt = baseFormDal.Select(tableName, sql); if (dt != null && dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { T t = new T(); Type type = t.GetType(); PropertyInfo[] infos = type.GetProperties(); if (infos.Length > 0) { foreach (PropertyInfo info in infos) { foreach (DataColumn dc in dt.Columns) { if (info.Name == dc.ColumnName) { if (info.PropertyType.Name.ToLower() == "string") { info.SetValue(t, dr[dc.ColumnName].ToString(), null); } else if (info.PropertyType.Name.ToLower() == "datetime") { if (string.IsNullOrWhiteSpace(dr[dc.ColumnName].ToString())) { //info.SetValue(t, new DateTime(), null); } else { DateTime date = DateTime.Parse(dr[dc.ColumnName].ToString()); info.SetValue(t, date, null); } } else if (info.PropertyType.Name.ToLower() == "boolean") { if (dr[dc.ColumnName].ToString() == "1") { info.SetValue(t, true, null); } else { info.SetValue(t, false, null); } } else if (info.PropertyType.Name.ToLower() == "double" || info.PropertyType.Name.ToLower() == "float") { info.SetValue(t, Convert.ToDouble(dr[dc.ColumnName]), null); } else if (info.PropertyType.Name.ToLower() == "decimal") { info.SetValue(t, Convert.ToDecimal(dr[dc.ColumnName]), null); } } } } } list.Add(t); } } return list; }View Code
毕设之数据库操作
标签: