第2012行到3013行代码 /// <summary> /// 根据表名得到表的主键 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public string GetPrimaryKey(String tableName) { DbConnection dcon = null; DbCommand dcom = null; try { string Pkey = ""; //sp_pkeys数据库系统自带的存储过程(查找表的主键); string str = " exec sp_pkeys " + "[" + tableName + "]"; dcon = Conn.getConn(); dcon.Open(); dcom = dcon.CreateCommand(); dcom.CommandText = str; DbDataReader dr = dcom.ExecuteReader(); if (dr.Read()) { Pkey = dr["COLUMN_NAME"].ToString(); } 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<T>(String tableName) { DbConnection dcon = null; DbCommand dcom = null; string Pkey = ""; try { //sp_pkeys数据库系统自带的存储过程(查找表的主键); string str = String.IsNullOrEmpty(tableName) ? " exec sp_pkeys " + "[" + typeof(T).Name + "]" : " exec sp_pkeys " + "[" + tableName + "]"; 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; } } } catch (Exception ex) { throw ex; } finally { dcom.Dispose(); dcon.Close(); dcon.Dispose(); } return Pkey; } #region IBaseDAO 成员 /// <summary> /// 保存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> public void Persist<T>(T t) { Insert<T>(t); } public void Persist<T>(String tableName, T t) { 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}", "[" + tableName + "]"); 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, t); 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 = t.GetType().GetProperty(pKey).PropertyType.GetMethod("Parse", new Type[] { typeof(string) }).Invoke(null, new object[] { id }); t.GetType().GetProperty(pKey).SetValue(t, idval, null); } catch (Exception ex) { throw ex; } finally { dcom.Dispose(); dcon.Close(); dcon.Dispose(); } } /// <summary> /// 带事务上下文的持久化方法 /// (该方法运行在调用者的事务上下文中,事务边界由调用者控制) /// (该方法不干涉连接的释放,否则可能事务异常) /// (优先采用tablename作为表名,若为空则采用范型类的类名作为表名) /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="ts">上下文事务</param> /// <param name="tableName">表名</param> /// <param name="t">要保存的对象</param> public void Persist<T>(DbTransaction ts,String tableName, T t) { using (DbCommand dcom = ts.Connection.CreateCommand()) { dcom.Transaction = ts; string SqlStr = "insert {tablename} ({namestr}) values({varstr})"; string pKey = GetPrimaryKey<T>(); SqlStr = SqlStr.Replace("{tablename}", "[" + (String.IsNullOrEmpty(tableName) ? typeof(T).Name : tableName) + "]"); 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, t); 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 = t.GetType().GetProperty(pKey).PropertyType.GetMethod("Parse", new Type[] { typeof(string) }).Invoke(null, new object[] { id }); t.GetType().GetProperty(pKey).SetValue(t, idval, nul