新增

/// <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. srcolltop 的用法

    document.body.scrollTop用法 网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可 ...

  2. 对ImageView.ScaleType的详解

    设置的方式有两种: 1.在layout.xml里面定义android:scaleType = "center" 2.在代码中调用imageview.setScaleType(Ima ...

  3. 夺命雷公狗—angularjs—25—angular内置的方法(高级)

    查看版本信息 angular.version console.log(angular.version); 判断是否相等 angular.equals() var str1 = ''; var str2 ...

  4. JSP-08-第三方控件的使用

    添加图片 下载 commons-fileupload-1.2.2.jar和commons-io-2.4.jar 导入项目 在添加涂抹的页面设置表单属性  enctype=”multipart/form ...

  5. JSP-03-实现数据传递

    会话跟踪:隐藏表单域.URL重写和Cookie 3.1 传参方法 get /post 区别   3.2  request 获取参数 数据类型  变量名  =  (数据类型)request.getPar ...

  6. 【linux】终端直接执行py文件,不需要python命令

    先将终端所在路径切换到python脚本文件的目录下然后给脚本文件运行权限,一般755就OK,如果完全是自己的私人电脑,也不做服务器什么的,给777的权限问题也不大(具体权限含义参考chmod指令的介绍 ...

  7. php+jquery注册实例

    写了一个简单的PHP+jQuery注册模块,需要填写的栏目包括用户名.邮箱.密码.重复密码和验证码,其中每个栏目需要具备的功能和要求如下图: 在做这个模块的时候,很大程度上借鉴了网易注册( http: ...

  8. 写sql语句分别按日,星期,月,季度,年统计

    --写sql语句分别按日,星期,月,季度,年统计销售额 --按日 ' group by day([date]) --按周quarter ' group by datename(week,[date]) ...

  9. Android ListView的理解(一)

    一.概述 这部分主要是讲解ListView在初始时,即setAdapter之后,如何将Adapter中取得的view添加到ListView中,主要涉及到几个方法: layoutChildren,fil ...

  10. nginx 反向代理 和lvs负载均衡

    nginx反向代理:用户请求nginx代理服务器然后代理服务器将用户请求转为服务器再由nginx代理服务器将服务器的响应反应给用户. lvs负载均衡:用户请求nginx代理服务器然后代理服务器将用户请 ...