示例#1
0
        /// <summary>
        /// 根据指定的数据连接对象查询数据表格
        /// </summary>
        /// <param name="dbConnection">一个有效的数据库连接对象</param>
        /// <param name="cmdType">命令类型 (存储过程,命令文本或其它)</param>
        /// <param name="cmdText">存储过程名称或T-SQL语句</param>
        /// <param name="dbParameters">执行命令所需要的数据库参数数组</param>
        /// <returns>返回查询到的数据表格(若无数据,此表格不会为null)</returns>
        public DataTable ExecuteDataTable(IDbConnection dbConnection, CommandType cmdType, string cmdText,
                                          params IDataParameter[] dbParameters)
        {
            if (dbConnection == null)
            {
                throw new ArgumentNullException("dbConnection");
            }

            var command  = DbCommandHelper.CreateCommand(_dbType, dbConnection, null, cmdType, cmdText, dbParameters);
            var dbAdpter = DbAdpterHelper.CreateAdapter(_dbType, command);

            var dataSet = new DataSet();

            dbAdpter.Fill(dataSet);

            return(dataSet.Tables.Count > 0 ? dataSet.Tables[0] : new DataTable());
        }
示例#2
0
        /// <summary>
        /// 执行指定数据库事务的命令,返回查询到的数据表格
        /// </summary>
        /// <param name="transaction">一个有效的连接事务</param>
        /// <param name="cmdType">命令类型 (存储过程,命令文本或其它)</param>
        /// <param name="cmdText">存储过程名称或T-SQL语句</param>
        /// <param name="dbParameters">执行命令所需要的数据库参数数组</param>
        /// <returns>返回查询到的数据表格(若无数据,此表格不会为null)</returns>
        public DataTable ExecuteDataTable(IDbTransaction transaction, CommandType cmdType, string cmdText,
                                          params IDataParameter[] dbParameters)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction != null && transaction.Connection == null)
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }

            var command = DbCommandHelper.CreateCommand(_dbType, transaction.Connection, transaction, cmdType, cmdText,
                                                        dbParameters);
            var dbAdpter = DbAdpterHelper.CreateAdapter(_dbType, command);

            var dataSet = new DataSet();

            dbAdpter.Fill(dataSet);

            return(dataSet.Tables.Count > 0 ? dataSet.Tables[0] : new DataTable());
        }