/// <summary> /// 通用新增方法 /// </summary> /// <param name="arr">一行数据封装的集合</param> /// <param name="tableName">表名</param> /// <returns>结果 为true成功</returns> public bool Insert(Dictionary<string, object> arr, string tableName) { SqlConnection con = new SqlConnection(ConnectionString); try { con.Open(); string sql = "insert into " + tableName + "("; string sql2 = ")values("; SqlCommand com = new SqlCommand(sql, con); foreach (KeyValuePair<string, object> ar in arr) { //跳过未赋值的属性 if (ar.Value == null) { continue; } sql += ar.Key + ","; sql2 += "@" + ar.Key + ","; SqlParameter sp = new SqlParameter("@" + ar.Key, ar.Value.GetType()); sp.Value = ar.Value; com.Parameters.Add(sp); } com.CommandText = sql.Substring(0, sql.Length - 1) + sql2.Substring(0, sql2.Length - 1) + ")"; return Convert.ToBoolean(com.ExecuteNonQuery()); } catch (Exception) { return false; } finally { if (con.State == ConnectionState.Open) con.Close(); } } /// <summary> /// 通用新增方法(批量) /// </summary> /// <param name="arr">多行数据封装的集合</param> /// <param name="tableName">表名</param> /// <returns>结果 为true 成功</returns> public bool Inserts(List<Dictionary<string, object>> arr, string tableName) { SqlConnection con = new SqlConnection(ConnectionString); SqlTransaction tr = con.BeginTransaction(); try { con.Open(); foreach (Dictionary<string, object> ar in arr) { string sql = "insert into " + tableName + "("; string sql2 = ")values("; SqlCommand com = new SqlCommand(sql, con); com.Transaction = tr; foreach (KeyValuePair<string, object> ak in ar) { //跳过未赋值的属性 if (ak.Value == null) { continue; } sql += ak.Key + ","; sql2 += "@" + ak.Key + ","; SqlParameter sp = new SqlParameter("@" + ak.Key, ak.Value.GetType()); sp.Value = ak.Value; com.Parameters.Add(sp); } com.CommandText = sql.Substring(0, sql.Length - 1) + sql2.Substring(0, sql2.Length - 1) + ")"; com.ExecuteNonQuery(); } tr.Commit(); return true; } catch (Exception) { tr.Rollback(); return false; } finally { if (con.State == ConnectionState.Open) con.Close(); } } /// <summary> /// 通用新增方法 /// </summary> /// <param name="entity">实体</param> /// <returns></returns> public bool Insert(object entity) { if (entity == null) { return false; } SqlConnection con = new SqlConnection(ConnectionString); try { con.Open(); Type t = entity.GetType(); string tableName = entity.GetType().Name; System.Reflection.PropertyInfo[] pars = t.GetProperties(); string sql = "insert into " + tableName + "("; string sql2 = ")values("; SqlCommand com = new SqlCommand(sql, con); foreach (System.Reflection.PropertyInfo p in pars) { //跳过未赋值的属性 if (p.GetValue(entity, null) == null) { continue; } sql += p.Name + ","; sql2 += "@" + p.Name + ","; SqlParameter sp = new SqlParameter("@" + p.Name, p.PropertyType); sp.Value = p.GetValue(entity, null); com.Parameters.Add(sp); } com.CommandText = sql.Substring(0, sql.Length - 1) + sql2.Substring(0, sql2.Length - 1) + ")"; return Convert.ToBoolean(com.ExecuteNonQuery()); } catch (Exception) { return false; } finally { if (con.State == ConnectionState.Open) con.Close(); } } /// <summary> /// 通用新增方法 /// </summary> /// <param name="entity">实体</param> /// <param name="tableName">表名</param> /// <returns></returns> public bool Insert(object entity, string tableName) { if (entity == null) { return false; } SqlConnection con = new SqlConnection(ConnectionString); try { con.Open(); Type t = entity.GetType(); System.Reflection.PropertyInfo[] pars = t.GetProperties(); string sql = "insert into " + tableName + "("; string sql2 = ")values("; SqlCommand com = new SqlCommand(sql, con); foreach (System.Reflection.PropertyInfo p in pars) { //跳过未赋值的属性 if (p.GetValue(entity, null) == null) { continue; } sql += p.Name + ","; sql2 += "@" + p.Name + ","; SqlParameter sp = new SqlParameter("@" + p.Name, p.PropertyType); sp.Value = p.GetValue(entity, null); com.Parameters.Add(sp); } com.CommandText = sql.Substring(0, sql.Length - 1) + sql2.Substring(0, sql2.Length - 1) + ")"; return Convert.ToBoolean(com.ExecuteNonQuery()); } catch (Exception) { return false; } finally { if (con.State == ConnectionState.Open) con.Close(); } } /// <summary> /// 通用新增方法(批量) /// </summary> /// <param name="entitys">批量实体集合</param> /// <returns></returns> public bool Inserts(List<object> entitys) { SqlConnection con = new SqlConnection(ConnectionString); SqlTransaction tr = con.BeginTransaction(); try { con.Open(); foreach (object entity in entitys) { if (entity == null) { continue; } Type t = entity.GetType(); string tableName = entity.GetType().Name; System.Reflection.PropertyInfo[] pars = t.GetProperties(); string sql = "insert into " + tableName + "("; string sql2 = ")values("; SqlCommand com = new SqlCommand(sql, con); com.Transaction = tr; foreach (System.Reflection.PropertyInfo p in pars) { //跳过未赋值的属性 if (p.GetValue(entity, null) == null) { continue; } sql += p.Name + ","; sql2 += "@" + p.Name + ","; SqlParameter sp = new SqlParameter("@" + p.Name, p.PropertyType); sp.Value = p.GetValue(entity, null); com.Parameters.Add(sp); } com.CommandText = sql.Substring(0, sql.Length - 1) + sql2.Substring(0, sql2.Length - 1) + ")"; } tr.Commit(); return true; } catch (Exception) { tr.Rollback(); return false; } finally { if (con.State == ConnectionState.Open) con.Close(); } } /// <summary> /// 通用新增方法(批量) /// </summary> /// <param name="entitys">批量实体集合</param> /// <param name="tableName">表名</param> /// <returns></returns> public bool Inserts(List<object> entitys, string tableName) { SqlConnection con = new SqlConnection(ConnectionString); ; SqlTransaction tr = con.BeginTransaction(); try { con.Open(); foreach (object entity in entitys) { if (entity == null) { continue; } Type t = entity.GetType(); System.Reflection.PropertyInfo[] pars = t.GetProperties(); string sql = "insert into " + tableName + "("; string sql2 = ")values("; SqlCommand com = new SqlCommand(sql, con); com.Transaction = tr; foreach (System.Reflection.PropertyInfo p in pars) { //跳过未赋值的属性 if (p.GetValue(entity, null) == null) { continue; } sql += p.Name + ","; sql2 += "@" + p.Name + ","; SqlParameter sp = new SqlParameter("@" + p.Name, p.PropertyType); sp.Value = p.GetValue(entity, null); com.Parameters.Add(sp); } com.CommandText = sql.Substring(0, sql.Length - 1) + sql2.Substring(0, sql2.Length - 1) + ")"; } tr.Commit(); return true; } catch (Exception) { tr.Rollback(); return false; } finally { if (con.State == ConnectionState.Open) con.Close(); } }