Пример #1
0
        /// <summary>
        /// 执行指定数据库连接对象的命令,返回结果集中的第一行第一列.
        /// </summary>
        /// <param name="dbConnection">一个有效的数据库连接对象</param>
        /// <param name="cmdType">命令类型 (存储过程,命令文本或其它)</param>
        /// <param name="cmdText">存储过程名称或T-SQL语句</param>
        /// <param name="dbParameters">执行命令所需要的数据库参数数组</param>
        /// <returns>返回结果集中的第一行第一列</returns>
        public object ExecuteScalar(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 result  = command.ExecuteScalar();

            command.Parameters.Clear();

            return(result);
        }
Пример #2
0
        /// <summary>
        /// 执行指定数据库连接字符串的数据阅读器
        /// </summary>
        /// <param name="dbConnection">一个有效的数据库连接对象</param>
        /// <param name="cmdType">命令类型 (存储过程,命令文本或其它)</param>
        /// <param name="cmdText">存储过程名或T-SQL语句</param>
        /// <param name="dbParameters">执行命令所需要的数据库参数</param>
        /// <returns>返回包含结果集的IDataReader对象</returns>
        public IDataReader ExecuteReader(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 dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);

            ClearCommandParameters(command);

            return(dataReader);
        }
Пример #3
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());
        }
Пример #4
0
        /// <summary>
        /// [调用者方式]执行指定数据库事务的数据阅读器,指定参数.
        /// </summary>
        /// <param name="transaction">一个有效的数据库事务对象</param>
        /// <param name="cmdType">数据库命令类型(存储过程、文本以及其它)</param>
        /// <param name="cmdText">存储过程名称或者SQL语句</param>
        /// <param name="dbParameters">执行命令所需要的参数数组</param>
        /// <returns>返回命令所影响的行数</returns>
        public IDataReader ExecuteReader(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 dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);

            ClearCommandParameters(command);

            return(dataReader);
        }
Пример #5
0
        /// <summary>
        /// 执行带事务的数据库命令
        /// </summary>
        /// <param name="transaction">一个有效的数据库事务对象</param>
        /// <param name="cmdType">数据库命令类型(存储过程、文本以及其它)</param>
        /// <param name="cmdText">存储过程名称或者SQL语句</param>
        /// <param name="dbParameters">执行命令所需要的参数数组</param>
        /// <returns>返回命令所影响的行数</returns>
        public int ExecuteNonQuery(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 returnVal = command.ExecuteNonQuery();

            command.Parameters.Clear();
            return(returnVal);
        }
Пример #6
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());
        }