这个仅是为了培训做的一个小例子
public class DB { public static string GetClassName(Type type) { if (type == null) throw new ArgumentException("参数type不能为空");
//if (type.HasAttribute<AliasAttribute>()) // return type.GetAttribute<AliasAttribute>().Name.ToLower(); //else return type.Name.ToLower(); } public static T AddObject<T>(T o) where T :class { string tablename = GetClassName(typeof(T)); string fieldName; object fieldValue; string[] fieldNames; object[] fieldValues; System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties(); fieldNames=new string[Props.Length ]; fieldValues =new object [Props.Length ]; string checkstr = ""; for (int i = 0; i < Props.Length; i++) { fieldName = Props[i].Name; fieldValue = Props[i].GetValue(o, null); if (fieldValue == null) continue; fieldValue = CommonFunction.SafeSQL(fieldValue.ToString()); fieldNames[i] = fieldName; fieldValues[i] = fieldValue; object[] attributes = Props[i].GetCustomAttributes(false); foreach (Attribute a in attributes) { //判断Attribute 中是否 为 ImgAttribute if (a is UniqueColumnAttribute) { checkstr = "not exists(select * from " +tablename + " where " + fieldName + "=‘" + fieldValue + "‘)"; } } } if (checkstr != "") { checkstr = "if("+checkstr +")"; } string insert = "" ,tail=""; insert = "insert into " + tablename + " ("; tail = "values("; for (int i = 0; i < Props.Length; i++) { fieldName = fieldNames[i] ;
if (fieldName.ToUpper() == "ID") continue;
object _fieldValue = fieldValues[i]; if (_fieldValue == null) { } else {
insert += fieldName + ",";
tail += "‘" + _fieldValue + "‘,"; } } insert = insert.Remove(insert.Length - 1); tail = tail.Remove(tail.Length - 1); string insertstr = insert + ")" + " " + tail + ")"; if (checkstr != "") insertstr = checkstr +"begin "+ insertstr+" end"; SqlHelper link = new SqlHelper(); int r= link.UpdateDataBase(insertstr);
//if (o.GetType == typeof(KeywordCat)) //{
//} //else if (o.GetType == typeof(Keyword)) //{ //} //else //{
//} if(r>0) return o; return null; } public static bool DelObject<T>( int id) where T : class { if (id == 0) return false; string tablename = GetClassName(typeof(T)); string insert = ""; insert = "delete from " + tablename + " where id="+id.ToString(); SqlHelper link = new SqlHelper(); int r = link.UpdateDataBase(insert); return true; } public static bool DelObject<T>(T o) where T : class { string tablename = GetClassName(typeof(T)); string fieldName; object fieldValue; string[] fieldNames; object[] fieldValues; System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties(); fieldNames = new string[Props.Length]; fieldValues = new object[Props.Length]; for (int i = 0; i < Props.Length; i++) { fieldName = Props[i].Name; fieldValue = Props[i].GetValue(o, null); fieldNames[i] = fieldName; fieldValues[i] = fieldValue; } string str=""; for (int i = 0; i < Props.Length; i++) { fieldName = fieldNames[i]; string _fieldValue = fieldValues[i].ToString(); if (fieldName.ToUpper() == "ID") { str = "delete from " + tablename + " where id=" + _fieldValue ; break; } } SqlHelper link = new SqlHelper(); int r = link.UpdateDataBase(str); return true; } public static bool UpdateObject<T>(T o) where T : class { string tablename = GetClassName(typeof(T)); string fieldName; object fieldValue; string[] fieldNames; object[] fieldValues; System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties(); fieldNames = new string[Props.Length]; fieldValues = new object[Props.Length]; for (int i = 0; i < Props.Length; i++) { if (Props[i].DeclaringType.IsValueType) { fieldName = Props[i].Name; fieldValue = Props[i].GetValue(o, null); fieldNames[i] = fieldName; fieldValues[i] = fieldValue; } else { fieldNames[i] = null; fieldValues[i] = null; } } string str = "update "+tablename +" set ",where=" where id="; int id = 0; for (int i = 0; i < Props.Length; i++) { fieldName = fieldNames[i]; object _fieldValue = fieldValues[i] ; if (fieldName == null) continue; if (fieldName.ToUpper() == "ID") { if (_fieldValue == null) { } else { int.TryParse(_fieldValue.ToString(), out id); //str = "delete from " + tablename + " where id=" + _fieldValue; where += id.ToString(); } //break; } else { if (_fieldValue == null) { } else str += fieldName + "=‘" + _fieldValue.ToString() + "‘,"; }
} if (id <= 0) return false; str = str.Remove(str.Length - 1); str += where; SqlHelper link = new SqlHelper(); int r = link.UpdateDataBase(str); return true;
} public static T SelObject<T>(int id) where T : class { if (id == 0) return null ; string tablename = GetClassName(typeof(T)); string insert = ""; insert = "select * from " + tablename + " where id=" + id.ToString(); SqlHelper link = new SqlHelper(); DataTable dt = link.SelectDataBase(insert); if (dt == null || dt.Rows.Count == 0) return null; Type type = typeof(T); Object obj = type.Assembly.CreateInstance(type.FullName); System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties(); for (int i = 0; i < Props.Length; i++) { Props[i].SetValue(obj, dt.Rows[0][Props[i].Name], null); } return (T)obj; } public static T SelObject<T>(BaseQuery query ) where T : class { if (query==null ) return null; string tablename = GetClassName(typeof(T)); if (tablename.EndsWith("Query")) tablename = tablename.Replace("Query",""); string fieldName; object fieldValue; string[] fieldNames; object[] fieldValues;
System.Reflection.PropertyInfo[] Props = query.GetType().GetProperties(); fieldNames = new string[Props.Length]; fieldValues = new object[Props.Length]; for (int i = 0; i < Props.Length; i++) { fieldName = Props[i].Name; fieldValue = Props[i].GetValue(query, null); fieldNames[i] = fieldName; fieldValues[i] = fieldValue; } string insert = "",where="1=1"; insert = "select * from " + tablename + " where " ; int id = 0; for (int i = 0; i < Props.Length; i++) { fieldName = fieldNames[i]; object _fieldValue = fieldValues[i]; if (fieldName.ToUpper() == "ID") { if (_fieldValue == null) { } else { int.TryParse(_fieldValue.ToString(), out id); //str = "delete from " + tablename + " where id=" + _fieldValue; where +="Id="+ id.ToString(); } //break; } else { if (_fieldValue == null) { } else { if (string.IsNullOrEmpty(_fieldValue.ToString())) { } else where += " and " + fieldName + "=‘" + _fieldValue.ToString() + "‘"; } }
}
insert = insert + where; SqlHelper link = new SqlHelper(); DataTable dt = link.SelectDataBase(insert); if (dt == null || dt.Rows.Count == 0) return null; Type type = typeof(T); Object obj = type.Assembly.CreateInstance(type.FullName ); System.Reflection.PropertyInfo[] Props2 = typeof(T).GetProperties();
for (int i = 0; i < Props2.Length; i++) { Props2[i].SetValue(obj, dt.Rows[0][Props2[i].Name], null); } return (T)obj; } public static T SelObject<T>(string where) where T : class { if (string.IsNullOrEmpty(where))//==null ) return null;
string tablename = GetClassName(typeof(T)); string insert = ""; insert = "select * from " + tablename + " where " + where; SqlHelper link = new SqlHelper(); DataTable dt = link.SelectDataBase(insert); if (dt == null || dt.Rows.Count == 0) return null; Type type = typeof(T); Object obj = type.Assembly.CreateInstance(type.FullName); System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties();
for (int i = 0; i < Props.Length; i++) { Props[i].SetValue(obj, dt.Rows[0][Props[i].Name], null); } return (T)obj; }
public static List<T> ListObjects<T>(string where) where T : class { try { if (string.IsNullOrEmpty(where)) where = "1=1"; string tablename = GetClassName(typeof(T)); string insert = ""; insert = "select * from " + tablename + " where " + where; SqlHelper link = new SqlHelper(); DataTable dt = link.SelectDataBase(insert); if (dt == null || dt.Rows.Count == 0) return null; Type type = typeof(T); List<T> list = new List<T>(); for (int row = 0; row < dt.Rows.Count; row++) { Object obj = type.Assembly.CreateInstance(type.FullName ); System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties();
for (int i = 0; i < Props.Length; i++) { Props[i].SetValue(obj, dt.Rows[row][Props[i].Name], null); } list.Add((T)obj); } return list; } catch { return null; } } }
原文:http://www.cnblogs.com/zhshlimi/p/4907162.html