You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1090 lines
42 KiB
C#

10 months ago
using System;
using System.Data;
using System.Collections.Generic;
using System.Data.Common;
using HcUtility.Comm;
using HcUtility.Logging;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.ComponentModel;
namespace HcUtility.Core
{
/// <summary>
/// 功能: 保存的底层处理
/// 创建: 吴伟 2009-05-08
///
/// </summary>
public class ModelObjectDB_Base : IModelObjectDB
{
#region Save
public DBResult Save(ModelObjectBase modData, string dbname = "")
{
DBResult result = new DBResult();
if (modData == null)
{
result.Success = false;
result.Message = "没有输入任何需要修改的数据";
}
List<ModelObjectBase> dataList = new List<ModelObjectBase>();
dataList.Add(modData);
return Save(dataList, dbname);
}
/// <summary>
/// 功能: 保存数据的统一处理
/// 创建: ddlucky 2017-03-01
/// 说明:
/// 输入参数:
/// dataList:保存数据的列表
/// 输出参数:(无)
/// 返回:
/// 返回DBResult类
/// .Success:true保存成功,false保存失败
/// .Message:返回的详细说明
///
///
/// </summary>
///
/* wuwei 2009-07-07: ()
* Data Access使
using (DbConnection connection = db.CreateConnection())
{
connection.Open();
DbTransaction transaction = connection.BeginTransaction();
try
{
// Credit the first account.
db.ExecuteNonQuery(creditCommand, transaction);
// Debit the second account.
db.ExecuteNonQuery(debitCommand, transaction);
// Commit the transaction.
transaction.Commit();
result = true;
}
catch
{
// Roll back the transaction.
transaction.Rollback();
}
connection.Close();
return result;
}
*/
public DBResult Save(List<ModelObjectBase> dataList, string dbname = "")
{
DBResult result = new DBResult();
result.Success = false;
result.Message = "保存数据出错";
//此主要用于检查前和检查后处理。
//在这些过程中生成的DbCommand因为需要事务控制不能在相应的方法中释放需要回传后在此过程中释放
List<DbCommand> cmdList = new List<DbCommand>();
Database db = DatabaseFactory.CreateDatabase();
if (dbname != "") db = DatabaseFactory.CreateDatabase(dbname);
using (DbConnection con = db.CreateConnection())
{
con.Open();
//创建事务
DbTransaction tran = con.BeginTransaction();
try
{
//保存前处理
result = BeforeSaveHandler(dataList, db, con, tran, ref cmdList);
if (!result.Success)
{
throw new ModelDbException(result.Message);
}
//保存数据
foreach (ModelObjectBase modeldata in dataList)
{
result = SaveHeadAndBodyList(cmdList, db, con, tran, modeldata);
if (!result.Success)
throw new ModelDbException(result.Message);
}
//保存后处理
result = AfterSaveHandler(dataList, db, con, tran, ref cmdList);
if (!result.Success)
{
throw new ModelDbException(result.Message);
}
tran.Commit();
result.Success = true;
result.Message = "数据保存成功";
LoggingUtil.Write(result.Message, LoggingCategory.General, LoggingPriority.Lowest);
}
catch (Exception e)
{
tran.Rollback();
result.Success = false;
if (!(e is ModelDbException))
{
result.Message = e.ToString();
}
LoggingUtil.Write(e.ToString(), LoggingCategory.General, LoggingPriority.Normal);
}
finally
{
foreach (DbCommand cmd in cmdList)
{
if (cmd != null)
cmd.Dispose();
}
con.Close();
}
}
return result;
}
private DBResult SaveHeadAndBodyList(List<DbCommand> cmdList, Database db, IDbConnection idbConn, IDbTransaction idbTran, ModelObjectBase modeldata)
{
DBResult result = new DBResult();
result.Success = false;
try
{
//主表保存
result = SaveHead(cmdList, db, idbConn, idbTran, modeldata);
if (!result.Success)
{
throw new ModelDbException(result.Message);
}
//明细表保存
if (modeldata.BodyList != null)
{
foreach (List<ModelObjectBase> moblist in modeldata.BodyList)
{
result = SaveBodyList(cmdList, db, idbConn, idbTran, moblist);
if (!result.Success)
{
throw new ModelDbException(result.Message);
}
}
}
result.Success = true;
result.Message = "保存成功";
}
catch (Exception se)
{
result.Message = se.Message;
result.Success = false;
}
return result;
}
private DBResult SaveHead(List<DbCommand> cmdList, Database db, IDbConnection idbConn, IDbTransaction idbTran, ModelObjectBase modeldata)
{
DBResult result = new DBResult();
result.Success = false;
try
{
DbCommand cmdHead = null;
if (modeldata.DbOperationType == DbOperationType.DbotIns)
{
cmdHead = db.GetSqlStringCommand(modeldata.GenInsertSql());
}
else if (modeldata.DbOperationType == DbOperationType.DbotUpd)
{
cmdHead = db.GetSqlStringCommand(modeldata.GenUpdateSql());
}
else
{
cmdHead = db.GetSqlStringCommand(modeldata.GenDeleteSql());
}
cmdList.Add(cmdHead);
cmdHead.Connection = (DbConnection)idbConn;
cmdHead.Transaction = (DbTransaction)idbTran;
SetDbParam(modeldata, db, cmdHead);
cmdHead.ExecuteNonQuery();
result.Success = true;
result.Message = "保存成功";
}
catch (Exception e)
{
result.Success = false;
if (e is System.Data.OracleClient.OracleException && e.Message.Contains("ORA-00001"))
{
result.Message = "数据保存出现异常:数据已经存在,请刷新后再试";
}
else
{
result.Message = "数据保存出现异常!" + e.ToString();
}
}
return result;
}
private DBResult SaveBodyList(List<DbCommand> cmdList, Database db, IDbConnection idbConn, IDbTransaction idbTran, List<ModelObjectBase> moblist)
{
DBResult result = new DBResult();
result.Success = false;
try
{
DbCommand cmdUpd = null;
DbCommand cmdIns = null;
DbCommand cmdDel = null;
foreach (ModelObjectBase mob in moblist)
{
//result= SaveHeadAndBodyList(cmdList, db,idbConn,idbTran, mob); //递归调用
//if (!result.Success)
// throw new Exception(result.Message);
switch (mob.DbOperationType)
{
case DbOperationType.DbotIns:
if (cmdIns == null)
{
cmdIns = db.GetSqlStringCommand(mob.GenInsertSql());
cmdIns.Connection = (DbConnection)idbConn;
cmdIns.Transaction = (DbTransaction)idbTran;
cmdList.Add(cmdIns);
}
SetDbParam(mob, db, cmdIns); //查看后台执行的sql语句中的参数数据
cmdIns.ExecuteNonQuery();
break;
case DbOperationType.DbotUpd:
if (cmdUpd == null)
{
cmdUpd = db.GetSqlStringCommand(mob.GenUpdateSql());
cmdUpd.Connection = (DbConnection)idbConn;
cmdUpd.Transaction = (DbTransaction)idbTran;
cmdList.Add(cmdUpd);
}
SetDbParam(mob, db, cmdUpd);
cmdUpd.ExecuteNonQuery();
break;
case DbOperationType.DbotDel:
if (cmdDel == null)
{
cmdDel = db.GetSqlStringCommand(mob.GenDeleteSql());
cmdDel.Connection = (DbConnection)idbConn;
cmdDel.Transaction = (DbTransaction)idbTran;
cmdList.Add(cmdDel);
}
SetDbParam(mob, db, cmdDel);
cmdDel.ExecuteNonQuery();
break;
}
foreach (List<ModelObjectBase> mobbodylist in mob.BodyList)
{
result = SaveBodyList(cmdList, db, idbConn, idbTran, mobbodylist);
if (!result.Success)
{
throw new ModelDbException(result.Message);
}
}
}
result.Success = true;
result.Message = "保存成功";
}
catch (Exception e)
{
result.Success = false;
result.Message = "保存出现异常!" + e.Message;
}
return result;
}
/// <summary>
/// 功能: 用于保存前的处理,继承类可以重写此方法
/// 创建: ddlucky 2017-03-01
/// 说明:
/// 输入参数:
/// dataList:保存数据的列表
/// db: Microsoft.Practices.EnterpriseLibrary.Data.Database操作类
/// idbConn: 数据库连接
/// idbTran: 生成的事务
/// listCmd: DbCommand的列表主要用于在自己的过程中生成的DbCommand因为事务控制不能直接释放,需要回传统一释放
/// 输出参数:(无)
/// 返回:
/// 返回DBResult类
/// .Success:true保存成功,false保存失败
/// .Message:返回的详细说明
///
/// </summary>
protected virtual DBResult BeforeSaveHandler(List<ModelObjectBase> dataList, Database db,
IDbConnection idbConn, IDbTransaction idbTran, ref List<DbCommand> listCmd)
{
DBResult result = new DBResult();
result.Success = true;
result.Message = "";
return result;
}
/// <summary>
/// 功能: 用于保存后的处理,继承类可以重写此方法
/// 创建: ddlucky 2017-03-01
/// 说明:
/// 输入参数:
/// dataList:保存数据的列表
/// db: Microsoft.Practices.EnterpriseLibrary.Data.Database操作类
/// idbConn: 数据库连接
/// idbTran: 生成的事务
/// listCmd: DbCommand的列表主要用于在自己的过程中生成的DbCommand因为事务控制不能直接释放,需要回传统一释放
/// 输出参数:(无)
/// 返回:
/// 返回DBResult类
/// .Success:true保存成功,false保存失败
/// .Message:返回的详细说明
/// </summary>
virtual protected DBResult AfterSaveHandler(List<ModelObjectBase> dataList, Database db,
IDbConnection idbConn, IDbTransaction idbTran, ref List<DbCommand> listCmd)
{
DBResult result = new DBResult();
result.Success = true;
result.Message = "";
return result;
}
#endregion
#region Delete
public DBResult Delete(ModelObjectBase modeldata, params string[] sqllist)
{
var result = new DBResult();
if (modeldata == null)
{
result.Success = false;
result.Message = "没有输入任何需要修改的数据";
return result;
}
modeldata.DbOperationType = DbOperationType.DbotDel;
Database db = DatabaseFactory.CreateDatabase();
using (DbConnection conn = db.CreateConnection())
{
conn.Open();
//创建事务
DbTransaction tran = conn.BeginTransaction();
try
{
foreach (var sql in sqllist)
{
var cmdbody = db.GetSqlStringCommand(sql);
db.ExecuteNonQuery(cmdbody, tran);
}
//删除数据
var cmd = db.GetSqlStringCommand(modeldata.GenDeleteSql());
SetDbParam(modeldata, db, cmd);
db.ExecuteNonQuery(cmd, tran);
tran.Commit();
result.Success = true;
result.Message = "删除数据成功";
LoggingUtil.Write(result.Message, LoggingCategory.General, LoggingPriority.Lowest);
}
catch (Exception e)
{
tran.Rollback();
result.Success = false;
if (!(e is ModelDbException))
{
result.Message = e.ToString();
}
LoggingUtil.Write(e.ToString(), LoggingCategory.General, LoggingPriority.Normal);
}
finally
{
conn.Close();
}
}
result.Success = true;
result.Message = "删除数据成功";
return result;
}
#endregion
#region Delete 并非删除而是将deleted置为1并记录时间和操作人
public DBResult Delete(ModelObjectBase modeldata, string USERID, bool updelete, params string[] sqllist)
{
var result = new DBResult();
if (modeldata == null)
{
result.Success = false;
result.Message = "没有输入任何需要修改的数据";
return result;
}
modeldata.DbOperationType = DbOperationType.DbotDel;
Database db = DatabaseFactory.CreateDatabase();
using (DbConnection conn = db.CreateConnection())
{
conn.Open();
//创建事务
DbTransaction tran = conn.BeginTransaction();
try
{
foreach (var sql in sqllist)
{
var cmdbody = db.GetSqlStringCommand(sql);
db.ExecuteNonQuery(cmdbody, tran);
}
//删除数据
var cmd = db.GetSqlStringCommand(modeldata.GenDeleteSql(USERID));
SetDbParam(modeldata, db, cmd);
db.ExecuteNonQuery(cmd, tran);
tran.Commit();
result.Success = true;
result.Message = "删除数据成功";
LoggingUtil.Write(result.Message, LoggingCategory.General, LoggingPriority.Lowest);
}
catch (Exception e)
{
tran.Rollback();
result.Success = false;
if (!(e is ModelDbException))
{
result.Message = e.ToString();
}
LoggingUtil.Write(e.ToString(), LoggingCategory.General, LoggingPriority.Normal);
}
finally
{
conn.Close();
}
}
result.Success = true;
result.Message = "删除数据成功";
return result;
}
#endregion
#region 设置sql语句当中的@字段值
virtual protected void SetDbParam(ModelObjectBase data, Database db, DbCommand cmd)
{
var paramName = String.Empty;
DbType dbType;
PropertyDescriptorCollection pdcList = TypeDescriptor.GetProperties(data);
cmd.Parameters.Clear();
foreach (PropertyDescriptor pdc in pdcList)
{
foreach (Attribute attr in pdc.Attributes)
{
if (attr is ModelDBAttribute)
{
ModelDBAttribute a = (ModelDBAttribute)attr;
if (data.DbOperationType == DbOperationType.DbotIns)
{
if (ModelDBOprationType.Insert == (ModelDBOprationType.Insert & a.MDBType))
{
paramName = a.InsParamName == String.Empty ? pdc.Name : a.InsParamName;
dbType = TypeConvertUtil.ConverTypeToDbType(pdc.PropertyType);
db.AddInParameter(cmd, paramName, dbType, pdc.GetValue(data));
}
}
else if (data.DbOperationType == DbOperationType.DbotUpd)
{
if (ModelDBOprationType.Edit == (ModelDBOprationType.Edit & a.MDBType))
{
paramName = a.UpdParamName == String.Empty ? pdc.Name : a.UpdParamName;
dbType = TypeConvertUtil.ConverTypeToDbType(pdc.PropertyType);
db.AddInParameter(cmd, paramName, dbType, pdc.GetValue(data));
}
}
else
{
if (ModelDBOprationType.Delete == (ModelDBOprationType.Delete & a.MDBType))
{
paramName = a.DelParamName == String.Empty ? pdc.Name : a.DelParamName;
dbType = TypeConvertUtil.ConverTypeToDbType(pdc.PropertyType);
db.AddInParameter(cmd, paramName, dbType, pdc.GetValue(data));
}
}
}
}
}
if (data.Extended())
{
foreach (var ed in data.ExtendDataList)
{
if (data.DbOperationType == DbOperationType.DbotIns)
{
if (ed.MDB.needInsert())
{
db.AddInParameter(cmd, ed.Name, DbType.String, ed.Value);
}
}
else if (data.DbOperationType == DbOperationType.DbotUpd)
{
if (ed.MDB.needEdit())
{
db.AddInParameter(cmd, ed.Name, DbType.String, ed.Value);
}
}
else
{
if (ed.MDB.needInsert())
{
db.AddInParameter(cmd, ed.Name, DbType.String, ed.Value);
}
}
}
}
}
#endregion
}
public class ModelObjectDB : ModelObjectDB_Base
{
#region Save
public DBResult Save(ModelObjectBase modData, string dbname = "")
{
DBResult result = new DBResult();
if (modData == null)
{
result.Success = false;
result.Message = "没有输入任何需要修改的数据";
}
List<ModelObjectBase> dataList = new List<ModelObjectBase>();
dataList.Add(modData);
return Save(dataList, dbname);
}
/// <summary>
/// 功能: 保存数据的统一处理
/// 创建: ddlucky 2017-03-01
/// 说明:
/// 输入参数:
/// dataList:保存数据的列表
/// 输出参数:(无)
/// 返回:
/// 返回DBResult类
/// .Success:true保存成功,false保存失败
/// .Message:返回的详细说明
//批量保存
//典型调用方法:
//var modb = new ModelObjectDB();
//var result = modb.Save(ModelObjectConvert<MsOpSeaeDetail>.ToModelObjectList(DelCtnList));
public DBResult Save(List<ModelObjectBase> dataList, string dbname = "")
{
DBResult result = new DBResult();
result.Success = false;
result.Message = "保存数据出错";
//此主要用于检查前和检查后处理。
//在这些过程中生成的DbCommand因为需要事务控制不能在相应的方法中释放需要回传后在此过程中释放
List<DbCommand> cmdList = new List<DbCommand>();
Database db = string.IsNullOrWhiteSpace(dbname)? DatabaseFactory.CreateDatabase(): DatabaseFactory.CreateDatabase(dbname);
//if (dbname != "") db = DatabaseFactory.CreateDatabase(dbname);
using (DbConnection con = db.CreateConnection())
{
con.Open();
//创建事务
DbTransaction tran = con.BeginTransaction();
try
{
//保存前处理
//此处主要进行时间标签的验证
result = BeforeSaveHandler(dataList, db, con, tran, ref cmdList);
if (!result.Success)
{
throw new ModelDbException(result.Message);
}
//保存数据
foreach (ModelObjectBase modeldata in dataList)
{
result = SaveHeadAndBodyList(cmdList, db, con, tran, modeldata);
if (!result.Success)
throw new ModelDbException(result.Message);
}
//保存后处理
//保存后处理用于执行一个存储过程,以进行一些业务存储之后的相关数据处理
//以后改到对象内进行配置 不在此处执行 以提高批量存储速度
//result = AfterSaveHandler(dataList, db, con, tran, ref cmdList);
//if (!result.Success)
//{
// throw new ModelDbException(result.Message);
//}
tran.Commit();
result.Success = true;
result.Message = "数据保存成功";
if(string.IsNullOrWhiteSpace(dbname))
LoggingUtil.Write(result.Message, LoggingCategory.General, LoggingPriority.Lowest);
}
catch (Exception e)
{
tran.Rollback();
result.Success = false;
if (!(e is ModelDbException))
{
result.Message = e.ToString();
}
LoggingUtil.Write(e.ToString(), LoggingCategory.General, LoggingPriority.Normal);
}
finally
{
foreach (DbCommand cmd in cmdList)
{
if (cmd != null)
cmd.Dispose();
}
con.Close();
}
}
return result;
}
private DBResult SaveHeadAndBodyList(List<DbCommand> cmdList, Database db, IDbConnection idbConn, IDbTransaction idbTran, ModelObjectBase modeldata)
{
DBResult result = new DBResult();
result.Success = false;
try
{
//主表保存
result = SaveHead(cmdList, db, idbConn, idbTran, modeldata);
if (!result.Success)
{
throw new ModelDbException(result.Message);
}
//明细表保存
if (modeldata.BodyList != null)
{
foreach (List<ModelObjectBase> moblist in modeldata.BodyList)
{
result = SaveBodyList(cmdList, db, idbConn, idbTran, moblist);
if (!result.Success)
{
throw new ModelDbException(result.Message);
}
}
}
result.Success = true;
result.Message = "保存成功";
}
catch (Exception se)
{
result.Message = se.Message;
result.Success = false;
}
return result;
}
private DBResult SaveHead(List<DbCommand> cmdList, Database db, IDbConnection idbConn, IDbTransaction idbTran, ModelObjectBase modeldata)
{
DBResult result = new DBResult();
result.Success = false;
try
{
DbCommand cmdHead = null;
if (modeldata.DbOperationType == DbOperationType.DbotIns)
{
cmdHead = db.GetSqlStringCommand(modeldata.GenInsertSql());
}
else if (modeldata.DbOperationType == DbOperationType.DbotUpd)
{
cmdHead = db.GetSqlStringCommand(modeldata.GenUpdateSql());
}
else
{
cmdHead = db.GetSqlStringCommand(modeldata.GenDeleteSql());
}
cmdList.Add(cmdHead);
cmdHead.Connection = (DbConnection)idbConn;
cmdHead.Transaction = (DbTransaction)idbTran;
SetDbParam(modeldata, db, cmdHead);
cmdHead.ExecuteNonQuery();
result.Success = true;
result.Message = "保存成功";
}
catch (Exception e)
{
result.Success = false;
if (e is System.Data.OracleClient.OracleException && e.Message.Contains("ORA-00001"))
{
result.Message = "数据保存出现异常:数据已经存在,请刷新后再试";
}
else
{
result.Message = "数据保存出现异常!" + e.ToString();
}
}
return result;
}
private DBResult SaveBodyList(List<DbCommand> cmdList, Database db, IDbConnection idbConn, IDbTransaction idbTran, List<ModelObjectBase> moblist)
{
DBResult result = new DBResult();
result.Success = false;
try
{
DbCommand cmdUpd = null;
DbCommand cmdIns = null;
DbCommand cmdDel = null;
foreach (ModelObjectBase mob in moblist)
{
//result= SaveHeadAndBodyList(cmdList, db,idbConn,idbTran, mob); //递归调用
//if (!result.Success)
// throw new Exception(result.Message);
switch (mob.DbOperationType)
{
case DbOperationType.DbotIns:
if (cmdIns == null)
{
cmdIns = db.GetSqlStringCommand(mob.GenInsertSql());
cmdIns.Connection = (DbConnection)idbConn;
cmdIns.Transaction = (DbTransaction)idbTran;
cmdList.Add(cmdIns);
}
SetDbParam(mob, db, cmdIns); //查看后台执行的sql语句中的参数数据
cmdIns.ExecuteNonQuery();
break;
case DbOperationType.DbotUpd:
if (cmdUpd == null)
{
cmdUpd = db.GetSqlStringCommand(mob.GenUpdateSql());
cmdUpd.Connection = (DbConnection)idbConn;
cmdUpd.Transaction = (DbTransaction)idbTran;
cmdList.Add(cmdUpd);
}
SetDbParam(mob, db, cmdUpd);
cmdUpd.ExecuteNonQuery();
break;
case DbOperationType.DbotDel:
if (cmdDel == null)
{
cmdDel = db.GetSqlStringCommand(mob.GenDeleteSql());
cmdDel.Connection = (DbConnection)idbConn;
cmdDel.Transaction = (DbTransaction)idbTran;
cmdList.Add(cmdDel);
}
SetDbParam(mob, db, cmdDel);
cmdDel.ExecuteNonQuery();
break;
}
foreach (List<ModelObjectBase> mobbodylist in mob.BodyList)
{
result = SaveBodyList(cmdList, db, idbConn, idbTran, mobbodylist);
if (!result.Success)
{
throw new ModelDbException(result.Message);
}
}
}
result.Success = true;
result.Message = "保存成功";
}
catch (Exception e)
{
result.Success = false;
result.Message = "保存出现异常!" + e.Message;
}
return result;
}
/// <summary>
/// 功能: 用于保存前的处理,继承类可以重写此方法
/// 创建: ddlucky 2017-03-01
/// 说明:
/// 输入参数:
/// dataList:保存数据的列表
/// db: Microsoft.Practices.EnterpriseLibrary.Data.Database操作类
/// idbConn: 数据库连接
/// idbTran: 生成的事务
/// listCmd: DbCommand的列表主要用于在自己的过程中生成的DbCommand因为事务控制不能直接释放,需要回传统一释放
/// 输出参数:(无)
/// 返回:
/// 返回DBResult类
/// .Success:true保存成功,false保存失败
/// .Message:返回的详细说明
///
/// </summary>
protected virtual DBResult BeforeSaveHandler(List<ModelObjectBase> dataList, Database db,
IDbConnection idbConn, IDbTransaction idbTran, ref List<DbCommand> listCmd)
{
DBResult result = new DBResult();
result.Success = true;
result.Message = "";
return result;
}
/// <summary>
/// 功能: 用于保存后的处理,继承类可以重写此方法
/// 创建: ddlucky 2017-03-01
/// 说明:
/// 输入参数:
/// dataList:保存数据的列表
/// db: Microsoft.Practices.EnterpriseLibrary.Data.Database操作类
/// idbConn: 数据库连接
/// idbTran: 生成的事务
/// listCmd: DbCommand的列表主要用于在自己的过程中生成的DbCommand因为事务控制不能直接释放,需要回传统一释放
/// 输出参数:(无)
/// 返回:
/// 返回DBResult类
/// .Success:true保存成功,false保存失败
/// .Message:返回的详细说明
/// </summary>
protected virtual DBResult AfterSaveHandler(List<ModelObjectBase> dataList, Database db,
IDbConnection idbConn, IDbTransaction idbTran, ref List<DbCommand> listCmd)
{
DBResult result = new DBResult();
result.Success = true;
result.Message = "";
return result;
}
#endregion
#region Delete
public DBResult Delete(ModelObjectBase modeldata, params string[] sqllist)
{
var result = new DBResult();
if (modeldata == null)
{
result.Success = false;
result.Message = "没有输入任何需要修改的数据";
return result;
}
modeldata.DbOperationType = DbOperationType.DbotDel;
Database db = DatabaseFactory.CreateDatabase();
using (DbConnection conn = db.CreateConnection())
{
conn.Open();
//创建事务
DbTransaction tran = conn.BeginTransaction();
try
{
foreach (var sql in sqllist)
{
var cmdbody = db.GetSqlStringCommand(sql);
db.ExecuteNonQuery(cmdbody, tran);
}
//删除数据
var cmd = db.GetSqlStringCommand(modeldata.GenDeleteSql());
SetDbParam(modeldata, db, cmd);
db.ExecuteNonQuery(cmd, tran);
tran.Commit();
result.Success = true;
result.Message = "删除数据成功";
LoggingUtil.Write(result.Message, LoggingCategory.General, LoggingPriority.Lowest);
}
catch (Exception e)
{
tran.Rollback();
result.Success = false;
if (!(e is ModelDbException))
{
result.Message = e.ToString();
}
LoggingUtil.Write(e.ToString(), LoggingCategory.General, LoggingPriority.Normal);
}
finally
{
conn.Close();
}
}
result.Success = true;
result.Message = "删除数据成功";
return result;
}
#endregion
#region Delete 并非删除而是将deleted置为1并记录时间和操作人
/// <summary>
/// ///Delete 并非删除而是将deleted置为1并记录时间和操作人
/// </summary>
/// <param name="modeldata"></param>
/// <param name="USERID"></param>
/// <param name="updelete"></param>
/// <param name="sqllist"></param>
/// <returns></returns>
public DBResult Delete(ModelObjectBase modeldata, string USERID, bool updelete, params string[] sqllist)
{
var result = new DBResult();
if (modeldata == null)
{
result.Success = false;
result.Message = "没有输入任何需要修改的数据";
return result;
}
modeldata.DbOperationType = DbOperationType.DbotDel;
Database db = DatabaseFactory.CreateDatabase();
using (DbConnection conn = db.CreateConnection())
{
conn.Open();
//创建事务
DbTransaction tran = conn.BeginTransaction();
try
{
foreach (var sql in sqllist)
{
var cmdbody = db.GetSqlStringCommand(sql);
db.ExecuteNonQuery(cmdbody, tran);
}
//删除数据
var cmd = db.GetSqlStringCommand(modeldata.GenDeleteSql(USERID));
SetDbParam(modeldata, db, cmd);
db.ExecuteNonQuery(cmd, tran);
tran.Commit();
result.Success = true;
result.Message = "删除数据成功";
LoggingUtil.Write(result.Message, LoggingCategory.General, LoggingPriority.Lowest);
}
catch (Exception e)
{
tran.Rollback();
result.Success = false;
if (!(e is ModelDbException))
{
result.Message = e.ToString();
}
LoggingUtil.Write(e.ToString(), LoggingCategory.General, LoggingPriority.Normal);
}
finally
{
conn.Close();
}
}
result.Success = true;
result.Message = "删除数据成功";
return result;
}
#endregion
#region 设置sql语句当中的@字段值
protected virtual void SetDbParam(ModelObjectBase data, Database db, DbCommand cmd)
{
var paramName = String.Empty;
DbType dbType;
PropertyDescriptorCollection pdcList = TypeDescriptor.GetProperties(data);
cmd.Parameters.Clear();
foreach (PropertyDescriptor pdc in pdcList)
{
foreach (Attribute attr in pdc.Attributes)
{
if (attr is ModelDBAttribute)
{
ModelDBAttribute a = (ModelDBAttribute)attr;
if (data.DbOperationType == DbOperationType.DbotIns)
{
if (ModelDBOprationType.Insert == (ModelDBOprationType.Insert & a.MDBType))
{
paramName = a.InsParamName == String.Empty ? pdc.Name : a.InsParamName;
dbType = TypeConvertUtil.ConverTypeToDbType(pdc.PropertyType);
db.AddInParameter(cmd, paramName, dbType, pdc.GetValue(data));
}
}
else if (data.DbOperationType == DbOperationType.DbotUpd)
{
if (ModelDBOprationType.Edit == (ModelDBOprationType.Edit & a.MDBType))
{
paramName = a.UpdParamName == String.Empty ? pdc.Name : a.UpdParamName;
dbType = TypeConvertUtil.ConverTypeToDbType(pdc.PropertyType);
db.AddInParameter(cmd, paramName, dbType, pdc.GetValue(data));
}
}
else
{
if (ModelDBOprationType.Delete == (ModelDBOprationType.Delete & a.MDBType))
{
paramName = a.DelParamName == String.Empty ? pdc.Name : a.DelParamName;
dbType = TypeConvertUtil.ConverTypeToDbType(pdc.PropertyType);
db.AddInParameter(cmd, paramName, dbType, pdc.GetValue(data));
}
}
}
}
}
if (data.Extended())
{
foreach (var ed in data.ExtendDataList)
{
if (data.DbOperationType == DbOperationType.DbotIns)
{
if (ed.MDB.needInsert())
{
db.AddInParameter(cmd, ed.Name, DbType.String, ed.Value);
}
}
else if (data.DbOperationType == DbOperationType.DbotUpd)
{
if (ed.MDB.needEdit())
{
db.AddInParameter(cmd, ed.Name, DbType.String, ed.Value);
}
}
else
{
if (ed.MDB.needDelete())
{
db.AddInParameter(cmd, ed.Name, DbType.String, ed.Value);
}
}
}
}
}
#endregion
}
}