第1510行到2012代码
public int Insert<T>(T obj) { DbConnection dcon = null; DbCommand dcom = null; try { dcon = Conn.getConn(); dcon.Open(); dcom = dcon.CreateCommand(); string SqlStr = "insert {tablename} ({namestr}) values({varstr})"; string pKey = GetPrimaryKey<T>(); SqlStr = SqlStr.Replace("{tablename}", "[" + typeof(T).Name + "]"); string varstr = ""; string namestr = ""; foreach (PropertyInfo pro in typeof(T).GetProperties()) { if (pro.Name.ToLower() != pKey.ToLower()) { namestr += "," + pro.Name; varstr += ",@" + pro.Name; DbParameter par = dcom.CreateParameter(); par.ParameterName = "@" + pro.Name; par.Value = ChargeValue(pro, obj); par.DbType = GetDbType(pro); dcom.Parameters.Add(par); } } varstr = varstr.Substring(1); namestr = namestr.Substring(1); SqlStr = SqlStr.Replace("{namestr}", namestr); SqlStr = SqlStr.Replace("{varstr}", varstr); dcom.CommandText = SqlStr; int i = dcom.ExecuteNonQuery(); dcom.Parameters.Clear(); dcom.CommandText = "select @@identity"; string id = dcom.ExecuteScalar().ToString(); object idval = obj.GetType().GetProperty(pKey).PropertyType.GetMethod("Parse", new Type[] { typeof(string) }).Invoke(null, new object[] { id }); obj.GetType().GetProperty(pKey).SetValue(obj, idval, null); return i; } catch (Exception ex) { throw ex; } finally { dcom.Dispose(); dcon.Close(); dcon.Dispose(); } } public int InsertWithPK<T>(T obj) { DbConnection dcon = null; DbCommand dcom = null; try { dcon = Conn.getConn(); dcon.Open(); dcom = dcon.CreateCommand(); string SqlStr = "insert {tablename} ({namestr}) values({varstr})"; string pKey = GetPrimaryKey<T>(); SqlStr = SqlStr.Replace("{tablename}", "[" + typeof(T).Name + "]"); string varstr = ""; string namestr = ""; foreach (PropertyInfo pro in typeof(T).GetProperties()) { namestr += "," + pro.Name; varstr += ",@" + pro.Name; DbParameter par = dcom.CreateParameter(); par.ParameterName = "@" + pro.Name; par.Value = ChargeValue(pro, obj); par.DbType = GetDbType(pro); dcom.Parameters.Add(par); } varstr = varstr.Substring(1); namestr = namestr.Substring(1); SqlStr = SqlStr.Replace("{namestr}", namestr); SqlStr = SqlStr.Replace("{varstr}", varstr); dcom.CommandText = SqlStr; int i = dcom.ExecuteNonQuery(); dcom.Parameters.Clear(); return i; } catch (Exception ex) { throw ex; } finally { dcom.Dispose(); dcon.Close(); dcon.Dispose(); } } public int InsertWithOutDefault<T>(T obj) { DbConnection dcon = null; DbCommand dcom = null; try { dcon = Conn.getConn(); dcon.Open(); dcom = dcon.CreateCommand(); string SqlStr = "insert {tablename} ({namestr}) values({varstr})"; string pKey = GetPrimaryKey<T>(); SqlStr = SqlStr.Replace("{tablename}", "[" + typeof(T).Name + "]"); string varstr = ""; string namestr = ""; foreach (PropertyInfo pro in typeof(T).GetProperties()) { if (pro.Name.ToLower() != pKey.ToLower()) { namestr += "," + pro.Name; varstr += ",@" + pro.Name; DbParameter par = dcom.CreateParameter(); par.ParameterName = "@" + pro.Name; par.Value = pro.GetValue(obj, null); par.DbType = GetDbType(pro); dcom.Parameters.Add(par); } } varstr = varstr.Substring(1); namestr = namestr.Substring(1); SqlStr = SqlStr.Replace("{namestr}", namestr); SqlStr = SqlStr.Replace("{varstr}", varstr); dcom.CommandText = SqlStr; int i = dcom.ExecuteNonQuery(); dcom.Parameters.Clear(); dcom.CommandText = "select @@identity"; string id = dcom.ExecuteScalar().ToString(); object idval = obj.GetType().GetProperty(pKey).PropertyType.GetMethod("Parse", new Type[] { typeof(string) }).Invoke(null, new object[] { id }); obj.GetType().GetProperty(pKey).SetValue(obj, idval, null); return i; } catch (Exception ex) { throw ex; } finally { dcom.Dispose(); dcon.Close(); dcon.Dispose(); } } public int Delete<T>(T obj) { DbConnection dcon = null; DbCommand dcom = null; try { dcon = Conn.getConn(); dcon.Open(); dcom = dcon.CreateCommand(); string SqlStr = "Delete {tablename} where {pkstr}"; string pKey = GetPrimaryKey<T>(); SqlStr = SqlStr.Replace("{tablename}", "[" + typeof(T).Name + "]"); SqlStr = SqlStr.Replace("{pkstr}", pKey + "=@" + pKey); DbParameter par = dcom.CreateParameter(); par.ParameterName = "@" + pKey; par.Value = FindProName<T>(pKey, obj).GetValue(obj, null); par.DbType = GetDbType(FindProName<T>(pKey, obj)); dcom.Parameters.Add(par); dcom.CommandText = SqlStr; int i = dcom.ExecuteNonQuery(); return i; } catch (Exception ex) { throw ex; } finally { dcom.Dispose(); dcon.Close(); dcon.Dispose(); } } /// <summary> /// 批量删除指定表的指定数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tableName"></param> /// <param name="oidList"></param> public void Remove(String tableName, List<Object> oidList) { if (!String.IsNullOrEmpty(tableName) && oidList != null && oidList.Count > 0) { SqlConnection con = new SqlConnection(SqlHelper._connString); SqlTransaction tranCxt = null; String delSQL = "DELETE FROM {tableName} WHERE {pkey}=@{pkeyValue}"; delSQL = delSQL.Replace("{tableName}",tableName); String pkName = GetPrimaryKey(tableName); delSQL = delSQL.Replace("{pkey}", pkName); delSQL = delSQL.Replace("{pkeyValue}", pkName); SqlParameter par = new SqlParameter(); par.ParameterName = "@"+pkName; try { con.Open(); tranCxt = con.BeginTransaction(); foreach (object pkObj in oidList) { par.Value = pkObj; SqlHelper.ExecuteNonQuery(tranCxt,CommandType.Text, delSQL, par); } tranCxt.Commit(); } catch (Exception) { tranCxt.Rollback(); throw; } finally { tranCxt.Dispose(); tranCxt = null; con.Close(); con.Dispose(); con = null; } } } /// <summary> /// 批量删除指定表的指定数据 /// (该方法运行在调用者的事务上下文中) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tableName"></param> /// <param name="oidList"></param> public void Remove(DbTransaction ts,String tableName, List<Object> oidList) { if (!String.IsNullOrEmpty(tableName) && oidList != null && oidList.Count > 0) { using (DbCommand dcom = ts.Connection.CreateCommand()) { String delSQL = "DELETE FROM {tableName} WHERE {pkey}=@{pkeyValue}"; delSQL = delSQL.Replace("{tableName}", tableName); String pkName = GetPrimaryKey(tableName); delSQL = delSQL.Replace("{pkey}", pkName); delSQL = delSQL.Replace("{pkeyValue}", pkName); SqlParameter par = new SqlParameter(); par.ParameterName = "@" + pkName; foreach (object pkObj in oidList) { par.Value = pkObj; dcom.Parameters.Clear(); dcom.CommandText = delSQL; dcom.Parameters.Add(par); dcom.ExecuteNonQuery(); } } } } /// <summary> /// 根据对象OID删除指定对象 /// (该方法运行调用者的事务上下文中) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="ts"></param> /// <param name="tableName"></param> /// <param name="obj"></param> /// <returns></returns> public int Remove<T>(DbTransaction ts,String tableName,T obj) { int retValue = 0; using (DbCommand cmd = ts.Connection.CreateCommand()) { cmd.Transaction = ts; string SqlStr = "Delete {tablename} where {pkstr}"; string pKey = GetPrimaryKey<T>(tableName); SqlStr = SqlStr.Replace("{tablename}", "[" + (String.IsNullOrEmpty(tableName)? typeof(T).Name : tableName)+ "]"); SqlStr = SqlStr.Replace("{pkstr}", pKey + "=@" + pKey); DbParameter par = cmd.CreateParameter(); par.ParameterName = "@" + pKey; par.Value = FindProName<T>(pKey, obj).GetValue(obj, null); par.DbType = GetDbType(FindProName<T>(pKey, obj)); cmd.CommandText = SqlStr; cmd.Parameters.Add(par); retValue = cmd.ExecuteNonQuery(); } return retValue; } public object ChargeValue<T>(PropertyInfo pro, T obj) { object val = pro.GetValue(obj, null); if (pro.PropertyType == typeof(int)) { if (val.ToString() == "0") val = DBNull.Value; } else if (pro.PropertyType == typeof(string)) { if (val == null) val = DBNull.Value; } else if (pro.PropertyType == typeof(decimal)) { if (val.ToString() == "0") val = DBNull.Value; } else if (pro.PropertyType == typeof(DateTime)) { if (val.ToString() == DateTime.MinValue.ToString()) val = DBNull.Value; } else if (pro.PropertyType == typeof(bool)) { if (val == null) val = DBNull.Value; } return val; } public int Update<T>(T obj) { DbConnection dcon = null; DbCommand dcom = null; try { dcon = Conn.getConn(); dcon.Open(); dcom = dcon.CreateCommand(); string SqlStr = "update {tablename} set {valuestr} where {pkstr}"; string pKey = GetPrimaryKey<T>(); SqlStr = SqlStr.Replace("{tablename}", "[" + typeof(T).Name + "]"); SqlStr = SqlStr.Replace("{pkstr}", pKey + "=@" + pKey); string valuestr = ""; foreach (PropertyInfo pro in typeof(T).GetProperties()) { if (pro.Name.ToLower() != pKey.ToLower()) { //如果类型是字符串并且值为空那么进入下一轮循环 if (pro.PropertyType == typeof(string) && ChargeValue(pro, obj) == DBNull.Value) { continue; } valuestr += "," + pro.Name + "=@" + pro.Name; } DbParameter par = dcom.CreateParameter(); par.ParameterName = "@" + pro.Name; par.Value = ChargeValue(pro, obj); par.DbType = GetDbType(pro); dcom.Parameters.Add(par); } valuestr = valuestr.Substring(1); SqlStr = SqlStr.Replace("{valuestr}", valuestr); dcom.CommandText = SqlStr; int i = dcom.ExecuteNonQuery(); dcom.Dispose(); dcon.Close(); dcon.Dispose(); return i; } catch (Exception ex) { throw ex; } finally { dcom.Dispose(); dcon.Close(); dcon.Dispose(); } } /// <summary> /// 获取变量的类型 /// </summary> /// <param name="pro"></param> /// <returns></returns> public DbType GetDbType(PropertyInfo pro) { DbType dt = DbType.Object; //Enum.GetValues(typeof(DbType)获取所有变量类型的集合 foreach (int value in Enum.GetValues(typeof(DbType))) { //判断值类型Enum.GetName(typeof(DbType), value) if (Enum.GetName(typeof(DbType), value) == pro.PropertyType.Name) dt = (DbType)value; } return dt; } /// <summary> /// 根据范型得到表的主键 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public string GetPrimaryKey<T>() { DbConnection dcon = null; DbCommand dcom = null; try { string Pkey = ""; string str = " exec sp_pkeys " + "[" + typeof(T).Name + "]";//sp_pkeys数据库系统自带的存储过程(查找表的主键) dcon = Conn.getConn(); dcon.Open(); dcom = dcon.CreateCommand(); dcom.CommandText = str; DbDataReader dr = dcom.ExecuteReader(); if (dr.Read()) { Pkey = dr["COLUMN_NAME"].ToString(); } foreach (PropertyInfo pro in typeof(T).GetProperties()) { if (pro.Name.ToLower() == Pkey.ToLower()) { Pkey = pro.Name; break; } } dr.Close(); dcom.Dispose(); dcon.Close(); dcon.Dispose(); return Pkey; } catch (Exception ex) { throw ex; } finally { dcom.Dispose(); dcon.Close(); dcon.Dispose(); } } /// <summary> /// 根据非范型得到表的主键 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public string GetPrimaryKey(Type type) { DbConnection dcon = null; DbCommand dcom = null; try { string Pkey = ""; string str = " exec sp_pkeys " + "[" + type.Name + "]";//sp_pkeys数据库系统自带的存储过程(查找表的主键) dcon = Conn.getConn(); dcon.Open(); dcom = dcon.CreateCommand(); dcom.CommandText = str; DbDataReader dr = dcom.ExecuteReader(); if (dr.Read()) { Pkey = dr["COLUMN_NAME"].ToString(); } foreach (PropertyInfo pro in type.GetProperties()) { if (pro.Name.ToLower() == Pkey.ToLower()) { Pkey = pro.Name; break; } } dr.Close(); dcom.Dispose(); dcon.Close(); dcon.Dispose(); return Pkey; } catch (Exception ex) { throw ex; } finally { dcom.Dispose(); dcon.Close(); dcon.Dispose(); } }