新增

/// <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(, sql.Length - ) + sql2.Substring(, sql2.Length - ) + ")"; 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(, sql.Length - ) + sql2.Substring(, sql2.Length - ) + ")";
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(, sql.Length - ) + sql2.Substring(, sql2.Length - ) + ")";
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(, sql.Length - ) + sql2.Substring(, sql2.Length - ) + ")";
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(, sql.Length - ) + sql2.Substring(, sql2.Length - ) + ")";
}
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(, sql.Length - ) + sql2.Substring(, sql2.Length - ) + ")";
}
tr.Commit();
return true;
}
catch (Exception)
{
tr.Rollback();
return false;
}
finally
{
if (con.State == ConnectionState.Open) con.Close();
}
}

修改

/// <summary>
/// 通用更新方法
/// </summary>
/// <param name="arr">要更新的数据集合(主键值必须存在于数据表中)</param>
/// <param name="tableName">表名</param>
/// <returns></returns>
public bool Update(Dictionary<string, object> arr, string tableName)
{
SqlConnection con = new SqlConnection(ConnectionString);
try
{
con.Open();
string sql = "UPDATE " + tableName + " SET ";
SqlCommand com = new SqlCommand(sql, con); foreach (KeyValuePair<string, object> ar in arr)
{
//跳过未赋值的属性
if (ar.Value == null)
{
continue;
}
SqlParameter sp = new SqlParameter("@" + ar.Key, ar.Value.GetType());
sp.Value = ar.Value;
com.Parameters.Add(sp);
//判断是否是主键
if (IsPrimaryKey(ar.Key, tableName))
{
continue;
}
sql += ar.Key + "=@" + ar.Key + ",";
}
sql = sql.Substring(, sql.Length - ) + " WHERE ";
List<Dictionary<string, object>> keys = Select("sp_pkeys " + tableName);
foreach (Dictionary<string, object> a in keys)
{
sql += a["COLUMN_NAME"] + "=@" + a["COLUMN_NAME"] + " and ";
}
com.CommandText = sql.Substring(, sql.Length - );
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></returns>
public bool Updates(List<Dictionary<string, object>> arr, string tableName)
{
SqlConnection con = new SqlConnection(ConnectionString);
SqlTransaction tr = con.BeginTransaction();
try
{
con.Open(); List<Dictionary<string, object>> keys = Select("sp_pkeys " + tableName); foreach (Dictionary<string, object> ar in arr)
{ string sql = "UPDATE " + tableName + " SET ";
SqlCommand com = new SqlCommand(sql, con);
com.Transaction = tr; foreach (KeyValuePair<string, object> a in ar)
{
//跳过未赋值的属性
if (a.Value == null)
{
continue;
}
SqlParameter sp = new SqlParameter("@" + a.Key, a.Value.GetType());
sp.Value = a.Value;
com.Parameters.Add(sp);
//判断是否是主键
if (IsPrimaryKey(a.Key, tableName))
{
continue;
}
sql += a.Key + "=@" + a.Key + ",";
}
sql = sql.Substring(, sql.Length - ) + " WHERE "; foreach (Dictionary<string, object> a in keys)
{
sql += a["COLUMN_NAME"] + "=@" + a["COLUMN_NAME"] + " and ";
} com.CommandText = sql.Substring(, sql.Length - );
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>
/// <param name="tableName">表名</param>
/// <returns></returns>
public bool Update(object entity, string tableName)
{
if (entity == null)
{
return false;
}
SqlConnection con = new SqlConnection(ConnectionString);
try
{
con.Open(); Type t = entity.GetType();
System.Reflection.PropertyInfo[] pt = t.GetProperties(); string sql = "UPDATE " + tableName + " SET ";
SqlCommand com = new SqlCommand(sql, con); foreach (System.Reflection.PropertyInfo p in pt)
{
//跳过未赋值的属性
if (p.GetValue(entity, null) == null)
{
continue;
} SqlParameter sp = new SqlParameter("@" + p.Name, p.PropertyType);
sp.Value = p.GetValue(entity, null);
com.Parameters.Add(sp);
//判断是否是主键
if (IsPrimaryKey(p.Name, tableName))
{
continue;
}
sql += p.Name + "=@" + p.Name + ",";
}
sql = sql.Substring(, sql.Length - ) + " WHERE "; List<Dictionary<string, object>> keys = Select("sp_pkeys " + tableName); foreach (Dictionary<string, object> a in keys)
{
sql += a["COLUMN_NAME"] + "=@" + a["COLUMN_NAME"] + " and ";
}
com.CommandText = sql.Substring(, sql.Length - );
return Convert.ToBoolean(com.ExecuteNonQuery());
}
catch (Exception)
{
return false;
}
finally
{
if (con.State == ConnectionState.Open) con.Close();
}
}
/// <summary>
/// 通用修改(实体)
/// </summary>
/// <param name="entity">封装一行数据的实体</param>
/// <returns></returns>
public bool Update(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[] pt = t.GetProperties(); string sql = "UPDATE " + tableName + " SET ";
SqlCommand com = new SqlCommand(sql, con); foreach (System.Reflection.PropertyInfo p in pt)
{
//跳过未赋值的属性
if (p.GetValue(entity, null) == null)
{
continue;
}
SqlParameter sp = new SqlParameter("@" + p.Name, p.PropertyType);
sp.Value = p.GetValue(entity, null);
com.Parameters.Add(sp);
//判断是否是主键
if (IsPrimaryKey(p.Name, tableName))
{
continue;
}
sql += p.Name + "=@" + p.Name + ",";
}
sql = sql.Substring(, sql.Length - ) + " WHERE "; List<Dictionary<string, object>> keys = Select("sp_pkeys " + tableName); foreach (Dictionary<string, object> a in keys)
{
sql += a["COLUMN_NAME"] + "=@" + a["COLUMN_NAME"] + " and ";
}
com.CommandText = sql.Substring(, sql.Length - );
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 Updates(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[] pt = t.GetProperties(); string sql = "UPDATE " + tableName + " SET ";
SqlCommand com = new SqlCommand(sql, con);
com.Transaction = tr;
foreach (System.Reflection.PropertyInfo p in pt)
{
//跳过未赋值的属性
if (p.GetValue(entity, null) == null)
{
continue;
}
SqlParameter sp = new SqlParameter("@" + p.Name, p.PropertyType);
sp.Value = p.GetValue(entity, null);
com.Parameters.Add(sp);
//判断是否是主键
if (IsPrimaryKey(p.Name, tableName))
{
continue;
}
sql += p.Name + "=@" + p.Name + ",";
}
sql = sql.Substring(, sql.Length - ) + " WHERE "; List<Dictionary<string, object>> keys = Select("sp_pkeys " + tableName); foreach (Dictionary<string, object> a in keys)
{
sql += a["COLUMN_NAME"] + "=@" + a["COLUMN_NAME"] + " and ";
}
com.CommandText = sql.Substring(, sql.Length - );
com.ExecuteNonQuery();
}
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 Updates(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[] pt = t.GetProperties(); string sql = "UPDATE " + tableName + " SET ";
SqlCommand com = new SqlCommand(sql, con);
com.Transaction = tr;
foreach (System.Reflection.PropertyInfo p in pt)
{
//跳过未赋值的属性
if (p.GetValue(entity, null) == null)
{
continue;
}
SqlParameter sp = new SqlParameter("@" + p.Name, p.PropertyType);
sp.Value = p.GetValue(entity, null);
com.Parameters.Add(sp);
//判断是否是主键
if (IsPrimaryKey(p.Name, tableName))
{
continue;
}
sql += p.Name + "=@" + p.Name + ",";
}
sql = sql.Substring(, sql.Length - ) + " WHERE "; List<Dictionary<string, object>> keys = Select("sp_pkeys " + tableName); foreach (Dictionary<string, object> a in keys)
{
sql += a["COLUMN_NAME"] + "=@" + a["COLUMN_NAME"] + " and ";
}
com.CommandText = sql.Substring(, sql.Length - );
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 UpdateWhereKeyIn(object entity, string ids)
{
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[] pt = t.GetProperties(); string sql = "UPDATE " + tableName + " SET ";
SqlCommand com = new SqlCommand(sql, con); foreach (System.Reflection.PropertyInfo p in pt)
{
//跳过未赋值的属性
if (p.GetValue(entity, null) == null)
{
continue;
}
//判断是否是主键
if (IsPrimaryKey(p.Name, tableName))
{
continue;
}
sql += p.Name + "=@" + p.Name + ",";
SqlParameter sp = new SqlParameter("@" + p.Name, p.PropertyType);
sp.Value = p.GetValue(entity, null);
com.Parameters.Add(sp);
}
sql = sql.Substring(, sql.Length - ) + " WHERE "; List<Dictionary<string, object>> keys = Select("sp_pkeys " + tableName);
foreach (Dictionary<string, object> a in keys)
{
sql += a["COLUMN_NAME"] + " in " + ids + " and ";
}
com.CommandText = sql.Substring(, sql.Length - );
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 UpdateWhereKeyIn(object entity, string ids, string tableName)
{
if (entity == null)
{
return false;
}
SqlConnection con = new SqlConnection(ConnectionString);
try
{
con.Open();
Type t = entity.GetType();
System.Reflection.PropertyInfo[] pt = t.GetProperties(); string sql = "UPDATE " + tableName + " SET ";
SqlCommand com = new SqlCommand(sql, con); foreach (System.Reflection.PropertyInfo p in pt)
{
//跳过未赋值的属性
if (p.GetValue(entity, null) == null)
{
continue;
}
//判断是否是主键
if (IsPrimaryKey(p.Name, tableName))
{
continue;
}
sql += p.Name + "=@" + p.Name + ",";
SqlParameter sp = new SqlParameter("@" + p.Name, p.PropertyType);
sp.Value = p.GetValue(entity, null);
com.Parameters.Add(sp);
}
sql = sql.Substring(, sql.Length - ) + " WHERE "; List<Dictionary<string, object>> keys = Select("sp_pkeys " + tableName); foreach (Dictionary<string, object> a in keys)
{
sql += a["COLUMN_NAME"] + " in " + ids + " and ";
}
com.CommandText = sql.Substring(, sql.Length - );
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></returns>
public bool Delete(Dictionary<string, object> arr, string tableName)
{
SqlConnection con = new SqlConnection(ConnectionString);
try
{
con.Open();
string sql = "DELETE FROM " + tableName + " WHERE ";
SqlCommand com = new SqlCommand(sql, con); foreach (KeyValuePair<string, object> ar in arr)
{
sql += ar.Key + "=@" + ar.Key + " and ";
SqlParameter sp = new SqlParameter("@" + ar.Key, ar.Value.GetType());
sp.Value = ar.Value;
com.Parameters.Add(sp);
}
com.CommandText = sql.Substring(, sql.Length - );
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></returns>
public bool Deletes(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 = "DELETE FROM " + tableName + " WHERE ";
SqlCommand com = new SqlCommand(sql, con);
com.Transaction = tr;
foreach (KeyValuePair<string, object> a in ar)
{
sql += a.Key + "=@" + a.Key + " and ";
SqlParameter sp = new SqlParameter("@" + a.Key, a.Value.GetType());
sp.Value = a.Value;
com.Parameters.Add(sp);
}
com.CommandText = sql.Substring(, sql.Length - );
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 Delete(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[] pr = t.GetProperties(); string sql = "DELETE FROM " + tableName + " WHERE ";
SqlCommand com = new SqlCommand(sql, con);
List<Dictionary<string, object>> keys = Select("sp_pkeys " + tableName); foreach (Dictionary<string, object> ar in keys)
{
sql += ar["COLUMN_NAME"] + "=@" + ar["COLUMN_NAME"] + " and ";
foreach (System.Reflection.PropertyInfo p in pr)
{
if (p.Name.Trim() == ar["COLUMN_NAME"].ToString().Trim())
{
SqlParameter sp = new SqlParameter("@" + ar["COLUMN_NAME"].ToString().Trim(), p.PropertyType);
sp.Value = p.GetValue(entity, null);
com.Parameters.Add(sp);
break;
}
}
} com.CommandText = sql.Substring(, sql.Length - );
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 Delete(object entity, string tableName)
{
if (entity == null)
{
return false;
}
SqlConnection con = new SqlConnection(ConnectionString);
try
{
con.Open();
Type t = entity.GetType();
System.Reflection.PropertyInfo[] pr = t.GetProperties(); string sql = "DELETE FROM " + tableName + " WHERE ";
SqlCommand com = new SqlCommand(sql, con);
List<Dictionary<string, object>> keys = Select("sp_pkeys " + tableName); foreach (Dictionary<string, object> ar in keys)
{
sql += ar["COLUMN_NAME"] + "=@" + ar["COLUMN_NAME"] + " and ";
foreach (System.Reflection.PropertyInfo p in pr)
{
if (p.Name.Trim() == ar["COLUMN_NAME"].ToString().Trim())
{
SqlParameter sp = new SqlParameter("@" + ar["COLUMN_NAME"].ToString().Trim(), p.PropertyType);
sp.Value = p.GetValue(entity, null);
com.Parameters.Add(sp);
break;
}
}
} com.CommandText = sql.Substring(, sql.Length - );
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 Deletes(List<object> entitys)
{
SqlConnection con = new SqlConnection(ConnectionString);
SqlTransaction tr = null;
try
{
con.Open();
tr = con.BeginTransaction(); foreach (object entity in entitys)
{
if (entity == null)
{
continue;
}
Type t = entity.GetType();
string tableName = entity.GetType().Name;
System.Reflection.PropertyInfo[] pt = t.GetProperties(); string sql = "DELETE FROM " + tableName + " WHERE ";
SqlCommand com = new SqlCommand(sql, con);
com.Transaction = tr;
List<Dictionary<string, object>> keys = Select("sp_pkeys " + tableName);
foreach (Dictionary<string, object> ar in keys)
{
sql += ar["COLUMN_NAME"] + "=@" + ar["COLUMN_NAME"] + " and ";
foreach (System.Reflection.PropertyInfo p in pt)
{ if (p.Name.Trim() == ar["COLUMN_NAME"].ToString().Trim())
{
SqlParameter sp = new SqlParameter("@" + p.Name, p.PropertyType);
sp.Value = p.GetValue(entity, null);
com.Parameters.Add(sp);
break;
}
}
}
com.CommandText = sql.Substring(, sql.Length - );
com.ExecuteNonQuery();
}
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 Deletes(List<object> entitys, string tableName)
{
SqlConnection con = new SqlConnection(ConnectionString);
SqlTransaction tr = null;
try
{
con.Open();
tr = con.BeginTransaction(); foreach (object entity in entitys)
{
if (entity == null)
{
continue;
}
Type t = entity.GetType();
System.Reflection.PropertyInfo[] pt = t.GetProperties(); string sql = "DELETE FROM " + tableName + " WHERE ";
SqlCommand com = new SqlCommand(sql, con);
com.Transaction = tr;
List<Dictionary<string, object>> keys = Select("sp_pkeys " + tableName);
foreach (Dictionary<string, object> ar in keys)
{
sql += ar["COLUMN_NAME"] + "=@" + ar["COLUMN_NAME"] + " and ";
foreach (System.Reflection.PropertyInfo p in pt)
{
if (p.Name.Trim() == ar["COLUMN_NAME"].ToString().Trim())
{
SqlParameter sp = new SqlParameter("@" + p.Name, p.PropertyType);
sp.Value = p.GetValue(entity, null);
com.Parameters.Add(sp);
break;
}
}
}
com.CommandText = sql.Substring(, sql.Length - );
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>
/// <param name="tableName">表名</param>
/// <returns></returns>
public bool DeleteWhereKeyIn(object entity, string ids)
{
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[] pr = t.GetProperties(); string sql = "DELETE FROM " + tableName + " WHERE ";
SqlCommand com = new SqlCommand(sql, con);
List<Dictionary<string, object>> keys = Select("sp_pkeys " + tableName); foreach (Dictionary<string, object> ar in keys)
{
sql += ar["COLUMN_NAME"] + " in " + ids + " and ";
}
com.CommandText = sql.Substring(, sql.Length - );
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 DeleteWhereKeyIn(object entity, string ids, string tableName)
{
if (entity == null)
{
return false;
}
SqlConnection con = new SqlConnection(ConnectionString);
try
{
con.Open(); Type t = entity.GetType();
System.Reflection.PropertyInfo[] pr = t.GetProperties(); string sql = "DELETE FROM " + tableName + " WHERE ";
SqlCommand com = new SqlCommand(sql, con);
List<Dictionary<string, object>> keys = Select("sp_pkeys " + tableName); foreach (Dictionary<string, object> ar in keys)
{
sql += ar["COLUMN_NAME"] + " in " + ids + " and ";
}
com.CommandText = sql.Substring(, sql.Length - );
return Convert.ToBoolean(com.ExecuteNonQuery());
}
catch (Exception)
{
return false;
}
finally
{
if (con.State == ConnectionState.Open) con.Close();
}
}

Reflection实现通用增删改的更多相关文章

  1. 用DBContext (EF) 实现通用增删改查的REST方法

    我们用ADO.NET Entity Data Model来生成实体类后,一般都会对这些类进行基本的增删改查操作,如果每个类都要写这些基本的方法,实在太乏味了.下面就是通过step by step的方式 ...

  2. EF学习笔记——通用增删改查方案

    http://blog.csdn.net/leftfist/article/details/25005307 我刚接触EF未久,还不知道它有什么强大之处,但看上去,EF提供了一般的增删改查功能.以往用 ...

  3. 关于EF 通用增删改查的封装

    1.  Entity Framework是Microsoft的ORM框架,随着 Entity Framework 不断的完善强化已经到达了EF 6.0+ 还是非常的完善的,目前使用的比例相对于其他OR ...

  4. Java连接GBase并封装增删改查

    1.介绍 GBase 是南大通用数据技术有限公司推出的自主品牌的数据库产品,目前在国内数据库市场具有较高的品牌知名度;GBase品牌的系列数据库都具有自己鲜明的特点和优势:GBase 8a 是国内第一 ...

  5. JDBC连接Greenplum数据库,封装了增删改查

    要启动好gp服务,再尝试连接 192.168.94.135是主节点(master)的ip 驱动Jar包在官网获取 嫌麻烦,可以直接用我在网盘分享的Jar包,版本较老 链接:https://pan.ba ...

  6. c#自定义ORM框架---(泛型&反射&实体类扩展属性<附带通用增、删、查、改>)

    该教材主要是运用到泛型.反射和实体类扩展属性 步骤一.建立扩展属性类 实体类扩展属性要继承Attribute基类完成 [AttributeUsage(AttributeTargets.Property ...

  7. jdbc的连接数据库,使用PreparedStatement实现增删改查等接口

    首先是连接,关闭资源等数据库操作 将连接数据库,关闭资源封装在JDBCUtils里 package jdbc.utils; import java.sql.Connection; import jav ...

  8. Entity Framework增删改之通用精简方法

    用EF用了好长一段时间了,从EF4.0的版本开始接触,感觉这个ORM不能说性能是最好的,但是我个人感觉功能实现以及和mvc的结合还是一个不错的企业级网站的解决方案,在此写个简易的通用扩展方法来方便大家 ...

  9. java中dao层的通用层,通过反射机制,操作数据库的增删改,适用的范围是不包含属性类

    这里首先必须注意的是:类的类名.字段必须与数据库中的表名和字段名保持一致,否则无法通过反射机制作出通用层 /** * 学生信息类,数据库中的StuInfo表 * */public class StuI ...

随机推荐

  1. MFC主窗口架构模型

    根据主窗口类型,MFC软件工程可以分为一下几种架构模型: 1.SDI(Simple Document Interface)单文档界面,一个主窗口下只编辑一份文档 2.MDI(Multiple Docu ...

  2. Ubuntu Firefox installs Flashplayer

    Adobe flash 下载(https://get.adobe.com/flashplayer/)  tar.gz版本(注:adobe 提供了yum,rpm,tar.gz和APT四种版本,yum和t ...

  3. "数学口袋精灵"bug的发现及单元测试

    1.项目内容: 团队项目:二次开发 至此,我们有了初步的与人合作经验,接下来投入到更大的团队中去. 也具备了一定的个人能力,能将自己的代码进行测试.接下来尝试在别人已有的基础上进行开发. 上一界51冯 ...

  4. maven的中传递依赖,maven的依赖管理(转)

    在maven的pom文件中 <dependencies> <dependency> <groupId>junit</groupId> <artif ...

  5. cookie的保存时间

    使用 .setMaxAge()设置(单位是秒) second>0 保存在硬盘上的时间 second<0 默认保存在浏览器的内存中 second=0 立即删除

  6. RMAN备份演练进阶篇

    前篇介绍了通过rman进行各种备份,进阶篇则主要是rman的一些功能扩展和增加功能,利用前篇你已经完全可以完成数据库的备份,而通过本篇你可以更好更方便的完成数据库的备份. 一.建立增量备份 如果数据库 ...

  7. linux中编译git时提示找不到ssl.h头文件

    在centos中的解决方案是安装一个叫 openssl-devel 的包.

  8. zw版【转发·台湾nvp系列Delphi例程】HALCON AffineTransRegion

    zw版[转发·台湾nvp系列Delphi例程]HALCON AffineTransRegion unit Unit1;interfaceuses Windows, Messages, SysUtils ...

  9. SQLServer中在视图上使用索引(转载)

    在SQL Server中,视图是一个保存的T-SQL查询.视图定义由SQL Server保存,以便它能够用作一个虚拟表来简化查询,并给基表增加另一层安全.但是,它并不占用数据库的任何空间.实际上,在你 ...

  10. Objective-C语言的面向对象特性

    Objective-C作为一种面向对象的编程语言,具有面向对象的基本特征,即:封装.继承和多态.主要介绍Objective-C中有关面向对象基本概念:类.对象.方法和属性等. 类是Objective- ...