前面簡單的修改功能已經實現了,下面是實現刪除功能,代碼1-1如下:
#region 刪除實體對應數據庫中的數據
public int Remove<T>(T entity)
{
TableInfo tableInfo = DbEntityUtils.GetTableInfo(entity, DbOperateType.DELETE);
string strSql = DbEntityUtils.GetDeleteByIdSql(tableInfo);
IDbDataParameter[] parms = DbFactory.CreateDbParameters(1);
parms[0].ParameterName = tableInfo.Id.Key;
parms[0].Value = tableInfo.Id.Value;
object val = AdoHelper.ExecuteNonQuery(transaction, CommandType.Text, strSql, parms);
return Convert.ToInt32(val);
}
#endregion
#region 根據主鍵id刪除實體對應數據庫中的數據
public int Remove<T>(object id) where T : new()
{
TableInfo tableInfo = DbEntityUtils.GetTableInfo(new T(), DbOperateType.DELETE);
string strSql = DbEntityUtils.GetDeleteByIdSql(tableInfo);
IDbDataParameter[] parms = DbFactory.CreateDbParameters(1);
parms[0].ParameterName = tableInfo.Id.Key;
parms[0].Value = id;
object val = AdoHelper.ExecuteNonQuery(transaction, CommandType.Text, strSql, parms);
return Convert.ToInt32(val);
}
#endregion
DbEntityUtils.GetDeleteByIdSql方法代碼如下1-2:
public static string GetDeleteByIdSql(TableInfo tableInfo)
{
string strSql = "delete {0} where {1} =" + AdoHelper.DbParmChar + tableInfo.Id.Key;
strSql = string.Format(strSql, tableInfo.TableName, tableInfo.Id.Key);
return strSql;
}