/// <summary> /// 实体类对象参数转换 /// </summary> /// <param name="sqlParams">现有参数</param> /// <param name="addParams">增加参数</param> /// <returns></returns> public static SqlParam[] AddParameter(SqlParam[] sqlParams,SqlParam[] addParams) { List<SqlParam> sqlparam = new List<SqlParam>(sqlParams); foreach (SqlParam param in addParams) { sqlparam.Add(new SqlParam("@" + param.FieldName, param.FieldValue)); } return sqlparam.ToArray(); }
public void Test() { IDataBase DbHelper = DataFactory.GetInstance(); for (int i = 0; i < 100; i++) { string AdminID = Public.GetGuID; string RoleID = Rand.Number(6); string AdminName = "潘向福"; string strSql = "INSERT INTO CNVP_Admin (AdminID,RoleID,AdminName) VALUES (@AdminID,@RoleID,@AdminName)"; SqlParam[] Param = new SqlParam[] { DbHelper.MakeParam("@AdminID",AdminID), DbHelper.MakeParam("@AdminName",AdminName), DbHelper.MakeParam("@RoleID",RoleID) }; DbHelper.ExecuteNonQuery(strSql, Param); } Test2(); }
/// <summary> /// 实体类对象参数转换 /// </summary> /// <param name="model">对象</param> /// <param name="sqlParams">参数</param> /// <returns></returns> public static SqlParam[] GetParameter(object model,SqlParam[] sqlParams) { DbType dbtype = new DbType(); Type type = model.GetType(); PropertyInfo[] props = type.GetProperties(); List<SqlParam> sqlparam = new List<SqlParam>(); foreach (PropertyInfo prop in props) { if (prop.GetValue(model, null) != null) { if (prop.PropertyType.ToString() == "System.Nullable`1[System.DateTime]") { dbtype = DbType.DateTime; } else { dbtype = DbType.AnsiString; } sqlparam.Add(new SqlParam("@" + prop.Name, dbtype, prop.GetValue(model, null))); } } if (sqlParams != null) { foreach (SqlParam param in sqlParams) { sqlparam.Add(new SqlParam(param.FieldName, param.FieldValue)); } } return sqlparam.ToArray(); }