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 ...
随机推荐
- 转:python webdriver API 之分页处理
对于 web 页面上的分页功能,我们一般做做以下操作: 获取总页数 翻页操作(上一页,下一页) 对于有些分页功能提供上一页,下一页按钮,以及可以输入具体页面数跳转功能不在本例的讨论范围. .. ...
- 13---Net基础加强
更新中,敬请期待............ 作业-复习 程序集1 程序集2 反射1 反射2 反射3 反射4-记事本插件1 反射5-记事本插件2
- 06---Net基础加强
字符串特性-池-不可变性 class Program { static void Main(string[] args) { #region 判断两个对象是否是同一个对象 //Person p1 = ...
- sql server添加列
alter table 表名 add 列名 数据类型如:alter table student add sex char(2)
- linux抓包方法
tcpdump -i eth0 -X -w data.cap 得到的包保存到本地wireshark解析ip.addr == url && http.request.url contai ...
- oracle随机取数据
select * from (select rownum,KEYWORD, CATEGORY,CREATE_DATE,UPDATE_DATE from (select * from knet_keyw ...
- 12.了解或熟悉 C, C++, .NET
挂起,暂不了解,不过C++略懂,.net虽不懂但是做过实际项目,有能力担当
- 夺命雷公狗---node.js---11之文件上传
我们在做文件上传前需要用npm来安装一个插件先, 首先打开项目所在的目录,然后按住shift键然后右键鼠标进入命令行安装formidable 然后开始编写上传的静态页面: <!DOCTYPE h ...
- PAT乙级 1009. 说反话 (20)
1009. 说反话 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一句英语,要求你编写程序,将句中 ...
- 下拉框分组显示optgroup
<select> <optgroup label="语言"> <option>中文</option> <option>英 ...