public static int UpdateModel <T>(string TableName, ref T model, string[] AutoUpdate, params string[] KeyColumn) { int num = -1; string str = ""; object obj2 = BMACache.Get("UpdateSql"); PropertyInfo[] properties = model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); MethodInfo[] methods = model.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance); if ((obj2 == null) || !((Dictionary <MethodInfo[], string>)obj2).Keys.Contains <MethodInfo[]>(methods)) { Dictionary <MethodInfo[], string> dictionary; StringBuilder builder = new StringBuilder("update " + TableName + " set "); StringBuilder builder2 = new StringBuilder(); foreach (PropertyInfo info in properties) { IEnumerable <Attribute> customAttributes = info.GetCustomAttributes(typeof(ModelAttribute)); if (((customAttributes.Count <Attribute>() > 0) && (customAttributes.First <Attribute>().GetType() == typeof(ModelAttribute))) && (((ModelAttribute)customAttributes.First <Attribute>()).Name == ModelAttributeType.TableColumn)) { string str2 = info.Name.ToLower(); if ((AutoUpdate == null) || !AutoUpdate.Contains <string>(str2)) { builder2.Append(str2 + "=?" + str2 + ","); } } } builder.Append(builder2.ToString().TrimEnd(new char[] { ',' })); builder.Append(" where "); bool flag = false; foreach (string str3 in KeyColumn) { flag = true; builder.Append(str3 + "=?" + str3); builder.Append(" and "); } if (flag) { str = builder.Remove(builder.Length - 5, 5).ToString(); } else { str = builder.ToString(); } if (obj2 == null) { dictionary = new Dictionary <MethodInfo[], string>(); } else { dictionary = (Dictionary <MethodInfo[], string>)obj2; } dictionary.Add(methods, str); BMACache.Insert("UpdateSql", dictionary); } else { str = ((Dictionary <MethodInfo[], string>)obj2)[methods]; } DynamicParameters param = new DynamicParameters(); param.AddDynamicParams((T)model); using (IDbConnection connection = GetMySqlOpenConnection(false)) { num = connection.Execute(str, param, null, null, null); connection.Close(); } return(num); }
public static int InsertModel <T>(string TableName, string[] AutoUpdateColumnName, ref T model) { int num = -1; string str = ""; object obj2 = BMACache.Get("InsertSql"); PropertyInfo[] properties = model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); MethodInfo[] methods = model.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance); if ((obj2 == null) || !((Dictionary <MethodInfo[], string>)obj2).Keys.Contains <MethodInfo[]>(methods)) { Dictionary <MethodInfo[], string> dictionary; StringBuilder builder = new StringBuilder("insert into " + TableName); StringBuilder builder2 = new StringBuilder(); foreach (PropertyInfo info in properties) { IEnumerable <Attribute> customAttributes = info.GetCustomAttributes(typeof(ModelAttribute)); if (((customAttributes.Count <Attribute>() > 0) && (customAttributes.First <Attribute>().GetType() == typeof(ModelAttribute))) && (((ModelAttribute)customAttributes.First <Attribute>()).Name == ModelAttributeType.TableColumn)) { string str2 = info.Name.ToLower(); if ((AutoUpdateColumnName == null) || !AutoUpdateColumnName.Contains <string>(str2)) { builder2.Append(str2); builder2.Append(","); } } } builder.Append("("); builder.Append(builder2.ToString().TrimEnd(new char[] { ',' })); builder.Append(") values(?"); builder.Append(builder2.ToString().TrimEnd(new char[] { ',' }).Replace(",", ",?")); builder.Append(")"); if ((AutoUpdateColumnName != null) && (AutoUpdateColumnName.Length > 0)) { builder.Append(";select ?NewInsertID=SCOPE_IDENTITY()");//;select @@IDENTITY //builder.Append(";select @@IDENTITY"); } str = builder.ToString(); if (obj2 == null) { dictionary = new Dictionary <MethodInfo[], string>(); } else { dictionary = (Dictionary <MethodInfo[], string>)obj2; } dictionary.Add(methods, str); BMACache.Insert("InsertSql", dictionary); } else { str = ((Dictionary <MethodInfo[], string>)obj2)[methods]; } DynamicParameters param = new DynamicParameters(); param.AddDynamicParams((T)model); if ((AutoUpdateColumnName != null) && (AutoUpdateColumnName.Length > 0)) { param.Add("?NewInsertID", null, DbType.Int32, ParameterDirection.Output, null); } using (IDbConnection connection = GetMySqlOpenConnection(false)) { num = connection.Execute(str, param, null, null, null); //num = connection.Query<int>(str, param, null, true, null, null).First<int>(); //num = connection.Query(str, param, null, null, null); connection.Close(); } if (((AutoUpdateColumnName != null) && (AutoUpdateColumnName.Length > 0)) && (num > 0)) { num = param.Get <int>("?NewInsertID"); } return(num); }