#region using System; using System.Data; using System.Data.Common; using System.Text; using CSFramework.DB; using CSFrameworkV5.Core; #endregion namespace CSFrameworkV5.DataAccess { /// /// 数据访问层基类定义 /// public class dalBase { /// /// 当前正在更新的数据集,调用Update方法时系统变量赋值 /// protected DataSet _CurrentDataSet4Update = null; /// /// 当前正在更新的数据表,调用Update方法时系统变量赋值 /// protected DataTable _CurrentTable4Update = null; protected IDatabase _Database; /// /// 是否生成带参数的SQL脚本,用于开发环境的调试。true:在调用DAL.Update方法时生成SQL脚本。 /// protected bool _GenerateLastRowSQLText = false; /// /// 最后一次提交数据时生成的SQL脚本,用于开发环境的调试 /// protected string _LastRowSQLText = ""; /// /// 当前登录的用户信息 /// protected Loginer _Loginer; /// /// 生成流水号码的类型(有规则流水号或GUID) /// protected UpdateKeyMode _UpdateSummaryKeyMode = UpdateKeyMode.None; /// /// 设置为受保护的构造器,不允许外部实例化 /// /// 当前登录的用户 protected dalBase(Loginer loginer) { if (loginer != null) { _Loginer = loginer; //若登录信息包含帐套编号,实例化该帐套对应的数据库连接 if (!string.IsNullOrEmpty(loginer.DBID)) _Database = DatabaseProvider.GetDatabase(loginer.DBID); } } /// /// 建立连接的数据库(预设为当前账套对应的数据库) /// public IDatabase Database { get => _Database; set => _Database = value; } /// /// SQL命令生成器的对象工厂 /// /// protected ICommandFactory GeneratorFactory => new SqlGeneratorFactory(_Database); /// /// 适配器提交记录时触发的事件 /// public virtual void AdapterRowUpdatingEvent(object sender, RowUpdatingEventArgs arg) { //生成SQL脚本 if (_GenerateLastRowSQLText) _LastRowSQLText = BuildSQLText(arg.Command.Parameters); //日期数据是DateTime.MinValue,需要转换为DBNull.Value FixDateTime(arg.Command.Parameters); } /// /// 生成带参数的SQL脚本 /// /// DbCommand参数 /// private string BuildSQLText(IDataParameterCollection parameters) { var sb = new StringBuilder(); foreach (DbParameter p in parameters) sb.AppendLine(p.ParameterName + "=" + (p.Value == null || p.Value == DBNull.Value || p.Value.ToStringEx() == "" ? "null" : p.Value.ToStringEx()) + ", "); return sb.ToStringEx(); } /// /// 将字段值为DateTime.MinValue转换为DBNULL /// /// private void FixDateTime(IDataParameterCollection parameters) { foreach (DbParameter p in parameters) { if (p.DbType != DbType.DateTime || p.DbType != DbType.DateTime2 || p.DbType != DbType.Time) continue; if (p.Value == DBNull.Value || p.Value == null) continue; //日期数据是DateTime.MinValue,需要转换为DBNull.Value; if (DateTime.Parse(p.Value.ToStringEx()).ToString("yyyyMMdd") == DateTime.MinValue.ToString("yyyyMMdd")) p.Value = DBNull.Value; } } #region 事务控制 /// /// true表示手工启用事务控制,预设为false,表示平台自动启动是事务。 /// protected bool _UserManualControlTrans = false; /// /// 当前事务 /// protected DbTransaction _CurrentTrans; /// /// 当前事务的数据库连接 /// protected DbConnection _CurrentConnection; /// /// 启用事务控制 /// protected virtual void BeginTransaction() { _CurrentConnection = _Database.CreateConnection(); _CurrentTrans = _CurrentConnection.BeginTransaction(); } /// /// 提交事务 /// protected virtual void CommitTransaction() { try { if (_CurrentTrans != null) { _CurrentTrans.Commit(); _CurrentTrans = null; _Database.Close(_CurrentConnection); } } catch { _Database.Close(_CurrentConnection); throw; } } /// /// 回滚事务 /// protected virtual void RollbackTransaction() { try { if (_CurrentTrans != null) { _CurrentTrans.Rollback(); _CurrentTrans = null; _Database.Close(_CurrentConnection); } } catch { _Database.Close(_CurrentConnection); throw; } } #endregion } }