Reflection实现通用增删改
新增
/// <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实现通用增删改的更多相关文章
- 用DBContext (EF) 实现通用增删改查的REST方法
我们用ADO.NET Entity Data Model来生成实体类后,一般都会对这些类进行基本的增删改查操作,如果每个类都要写这些基本的方法,实在太乏味了.下面就是通过step by step的方式 ...
- EF学习笔记——通用增删改查方案
http://blog.csdn.net/leftfist/article/details/25005307 我刚接触EF未久,还不知道它有什么强大之处,但看上去,EF提供了一般的增删改查功能.以往用 ...
- 关于EF 通用增删改查的封装
1. Entity Framework是Microsoft的ORM框架,随着 Entity Framework 不断的完善强化已经到达了EF 6.0+ 还是非常的完善的,目前使用的比例相对于其他OR ...
- Java连接GBase并封装增删改查
1.介绍 GBase 是南大通用数据技术有限公司推出的自主品牌的数据库产品,目前在国内数据库市场具有较高的品牌知名度;GBase品牌的系列数据库都具有自己鲜明的特点和优势:GBase 8a 是国内第一 ...
- JDBC连接Greenplum数据库,封装了增删改查
要启动好gp服务,再尝试连接 192.168.94.135是主节点(master)的ip 驱动Jar包在官网获取 嫌麻烦,可以直接用我在网盘分享的Jar包,版本较老 链接:https://pan.ba ...
- c#自定义ORM框架---(泛型&反射&实体类扩展属性<附带通用增、删、查、改>)
该教材主要是运用到泛型.反射和实体类扩展属性 步骤一.建立扩展属性类 实体类扩展属性要继承Attribute基类完成 [AttributeUsage(AttributeTargets.Property ...
- jdbc的连接数据库,使用PreparedStatement实现增删改查等接口
首先是连接,关闭资源等数据库操作 将连接数据库,关闭资源封装在JDBCUtils里 package jdbc.utils; import java.sql.Connection; import jav ...
- Entity Framework增删改之通用精简方法
用EF用了好长一段时间了,从EF4.0的版本开始接触,感觉这个ORM不能说性能是最好的,但是我个人感觉功能实现以及和mvc的结合还是一个不错的企业级网站的解决方案,在此写个简易的通用扩展方法来方便大家 ...
- java中dao层的通用层,通过反射机制,操作数据库的增删改,适用的范围是不包含属性类
这里首先必须注意的是:类的类名.字段必须与数据库中的表名和字段名保持一致,否则无法通过反射机制作出通用层 /** * 学生信息类,数据库中的StuInfo表 * */public class StuI ...
随机推荐
- Leetcode: String to Integer
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 超炫的3D HTML源代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- C#: MessageBox
系统学习下Form里用到最多的MessageBox MessageBox有21个重载的Show函数...都是返回DialogResult public enum DialogResult { None ...
- C++之路起航——标准模板库(deque)
deque(双端队列):http://baike.baidu.com/link?url=JTvA2cuLubptctHZwFxswvlZvxNdFOxmifsYCGLj5IZF-Tj4rbWLv8Jn ...
- 接口是否可继承接口? 抽像类是否可实现(implements)接口? 抽像类是否可继承实体类(concrete class)?
接口是否可继承接口? 抽像类是否可实现(implements)接口? 抽像类是否可继承实体类(concrete class)? 1. 接口可以继承接口. 2. 抽像类可以实现(implements)接 ...
- 服务器 CentOS上yum安装Nginx服务
一.更改yum源为网易的源加快速度 vi /etc/yum.repos.d/CentOS-Base.repo 更改内容如下 # CentOS-Base.repo # # This file uses ...
- 04---Net基础加强
字符串常用方法: 属性: Length获取字符串中字符的个数 IsNullOrEmpty() 静态方法,判断为null或者为“” ToCharArray() 将string转换为char[] To ...
- form文件上传,防止页面刷新
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>文件上传 ...
- haskell笔记1
haskell platform下载:https://www.haskell.org/platform/ 进入haskell控制台,终端输入 $ ghci 编译文件 :l file.hs 数组操作 & ...
- Linux 安装 nginx注意
./configure --prefix=/usr/local/nginx TO ./configure --prefix=/usr/local/nginx --conf-path=/usr/loca ...