/// <summary> /// 填充数据表 /// </summary> /// <param name="dt">目标数据表</param> /// <param name="commandText">sql查询</param> /// <param name="dbParameters">参数集</param> /// <param name="commandType">命令分类</param> /// <returns>数据表</returns> public virtual DataTable Fill(DataTable dt, string commandText, IDbDataParameter[] dbParameters, CommandType commandType) { // 自动打开 if (this.DbConnection == null) { this.Open(); this.MustCloseConnection = true; } else if (this.DbConnection.State == ConnectionState.Closed) { this.Open(); this.MustCloseConnection = true; } using (this.dbCommand = this.DbConnection.CreateCommand()) { this.dbCommand.CommandTimeout = this.DbConnection.ConnectionTimeout; this.dbCommand.CommandText = commandText; this.dbCommand.CommandType = commandType; if (this.dbTransaction != null) { this.dbCommand.Transaction = this.dbTransaction; } this.dbDataAdapter = this.GetInstance().CreateDataAdapter(); this.dbDataAdapter.SelectCommand = this.dbCommand; if ((dbParameters != null) && (dbParameters.Length > 0)) { for (int i = 0; i < dbParameters.Length; i++) { if (dbParameters[i] != null) { this.dbCommand.Parameters.Add(((ICloneable)dbParameters[i]).Clone()); } } // this.dbCommand.Parameters.AddRange(dbParameters); } this.dbDataAdapter.Fill(dt); SetBackParamValue(dbParameters); this.dbDataAdapter.SelectCommand.Parameters.Clear(); } // 自动关闭 if (this.MustCloseConnection) { this.Close(); } // 写入日志 //this.WriteLog(commandText); // 记录日志 SQLTrace.WriteLog(commandText, dbParameters); return(dt); }
/// <summary> /// 执行查询 /// </summary> /// <param name="commandText">sql查询</param> /// <param name="dbParameters">参数集</param> /// <param name="commandType">命令分类</param> /// <returns>结果集流</returns> public virtual IDataReader ExecuteReader(string commandText, IDbDataParameter[] dbParameters, CommandType commandType) { // 自动打开 if (this.DbConnection == null) { this.Open(); this.MustCloseConnection = true; } else if (this.DbConnection.State == ConnectionState.Closed) { this.Open(); this.MustCloseConnection = true; } this.dbCommand = this.DbConnection.CreateCommand(); this.dbCommand.CommandTimeout = this.DbConnection.ConnectionTimeout; this.dbCommand.CommandText = commandText; this.dbCommand.CommandType = commandType; if (this.dbTransaction != null) { this.dbCommand.Transaction = this.dbTransaction; } if (dbParameters != null) { this.dbCommand.Parameters.Clear(); for (int i = 0; i < dbParameters.Length; i++) { if (dbParameters[i] != null) { this.dbCommand.Parameters.Add(((ICloneable)dbParameters[i]).Clone()); } } } // 这里要关闭数据库才可以的 DbDataReader dbDataReader = null; if (!this.MustCloseConnection) { dbDataReader = this.dbCommand.ExecuteReader(); } else { dbDataReader = this.dbCommand.ExecuteReader(CommandBehavior.CloseConnection); } // 写入日志 // this.WriteLog(commandText); SQLTrace.WriteLog(commandText, dbParameters); return(dbDataReader); }