Inheritance: System.MarshalByRefObject, IDbTransaction
Exemplo n.º 1
0
        public DB2Transaction InternalBeginTransaction(IsolationLevel isolationLevel)
        {
            if ((refTransaction != null) && (refTransaction.IsAlive))
            {
                throw new InvalidOperationException("Cannot open another transaction");
            }
            if (State != ConnectionState.Open)
            {
                throw new InvalidOperationException("BeginTransaction needs an open connection");
            }

            if (refTransaction != null)
            {
                if (refTransaction.IsAlive)
                {
                    throw new InvalidOperationException("Parallel transactions not supported");
                }

                RollbackDeadTransaction();
                refTransaction = null;
            }
            transactionOpen = true;
            DB2Transaction tran = new DB2Transaction(this, isolationLevel);

            refTransaction = new WeakReference(tran);
            return(tran);
        }
Exemplo n.º 2
0
        public override void Close()
        {
            DB2Transaction transaction = null;

            if (refTransaction != null)
            {
                transaction = (DB2Transaction)refTransaction.Target;
            }
            if ((transaction != null) && refTransaction.IsAlive)
            {
                transaction.Dispose();
            }
            if (refCommands != null)
            {
                for (int i = 0; i < refCommands.Count; i++)
                {
                    DB2Command command = null;
                    if (refCommands[i] != null)
                    {
                        command = (DB2Command)((WeakReference)refCommands[i]).Target;
                    }
                    if ((command != null) && ((WeakReference)refCommands[i]).IsAlive)
                    {
                        try
                        {
                            command.ConnectionClosed();
                        }
                        catch { }
                    }
                    //?? refCommands[i] = null;
                }
            }

            InternalClose();
        }
Exemplo n.º 3
0
 public DB2Command(string commandStr, DB2Connection con, DB2Transaction trans)
 {
     commandText = commandStr;
     db2Conn     = con;
     db2Trans    = trans;
     if (con != null)
     {
         con.AddCommand(this);
     }
 }
Exemplo n.º 4
0
		public DB2Command (string commandStr, DB2Connection con, DB2Transaction trans)
		{
			commandText = commandStr;
			db2Conn = con;
			db2Trans = trans;
			if(con != null)
			{
				con.AddCommand(this);
			}
		}
Exemplo n.º 5
0
        internal void ConnectionClosed()
        {
            DB2DataReader reader = null;

            if ((refDataReader != null) && refDataReader.IsAlive)
            {
                reader = (DB2DataReader)refDataReader.Target;
            }
            if ((reader != null) && refDataReader.IsAlive)
            {
                reader.Dispose();
                refDataReader = null;
            }
            CloseStatementHandle(true);

            db2Trans = null;
        }
Exemplo n.º 6
0
 /// <summary>
 /// 执行指定数据库事务的命令,映射数据表并填充数据集,指定参数.
 /// </summary>
 /// <remarks>
 /// 示例:  
 ///  FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new DB2Parameter("@prodid", 24));
 /// </remarks>
 /// <param name="transaction">一个有效的连接事务</param>
 /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 /// <param name="commandText">存储过程名称或T-SQL语句</param>
 /// <param name="dataSet">要填充结果集的DataSet实例</param>
 /// <param name="tableNames">表映射的数据表数组
 /// 用户定义的表名 (可有是实际的表名.)
 /// </param>
 /// <param name="commandParameters">分配给命令的DB2Paramter参数数组</param>
 public static void FillDataset(DB2Transaction transaction, CommandType commandType,
     string commandText, DataSet dataSet, string[] tableNames,
     params DB2Parameter[] commandParameters)
 {
     FillDataset(transaction.Connection, transaction, commandType, commandText, dataSet, tableNames, commandParameters);
 }
Exemplo n.º 7
0
        /// <summary>
        /// [调用者方式]执行指定数据库事务的数据阅读器,指定参数.
        /// </summary>
        /// <remarks>
        /// 示例:  
        ///   DB2DataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders", new DB2Parameter("@prodid", 24));
        /// </remarks>
        /// <param name="transaction">一个有效的连接事务</param>
        /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
        /// <param name="commandText">存储过程名称或T-SQL语句</param>
        /// <param name="commandParameters">分配给命令的DB2Paramter参数数组</param>
        /// <returns>返回包含结果集的DB2DataReader</returns>
        public static DB2DataReader ExecuteReader(DB2Transaction transaction, CommandType commandType, string commandText, params DB2Parameter[] commandParameters)
        {
            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");

            return ExecuteReader(transaction.Connection, transaction, commandType, commandText, commandParameters, DB2ConnectionOwnership.External);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 执行指定连接数据库事物的存储过程,使用DataRow做为参数值,返回受影响的行数.
        /// </summary>
        /// <param name="transaction">一个有效的连接事务 object</param>
        /// <param name="spName">存储过程名称</param>
        /// <param name="dataRow">使用DataRow作为参数值</param>
        /// <returns>返回影响的行数</returns>
        public static int ExecuteNonQueryTypedParams(DB2Transaction transaction, String spName, DataRow dataRow)
        {
            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");
            if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

            // Sf the row has values, the store procedure parameters must be initialized
            if (dataRow != null && dataRow.ItemArray.Length > 0)
            {
                // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
                DB2Parameter[] commandParameters = DB2HelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

                // 分配参数值
                AssignParameterValues(commandParameters, dataRow);

                return DB2Helper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
            }
            else
            {
                return DB2Helper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 执行带事务的DB2Command(指定参数).
        /// </summary>
        /// <remarks>
        /// 示例:  
        ///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new db2Parameter("@prodid", 24));
        /// </remarks>
        /// <param name="transaction">一个有效的数据库连接对象</param>
        /// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param>
        /// <param name="commandText">存储过程名称或T-SQL语句</param>
        /// <param name="commandParameters">DB2Paramter参数数组</param>
        /// <returns>返回影响的行数</returns>
        public static int ExecuteNonQuery(DB2Transaction transaction, CommandType commandType, string commandText, params DB2Parameter[] commandParameters)
        {
            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");

            // 预处理
            DB2Command cmd = new DB2Command();
            bool mustCloseConnection = false;
            PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);

            // 执行
            int retval = cmd.ExecuteNonQuery();

            // 清除参数集,以便再次使用.
            cmd.Parameters.Clear();
            return retval;
        }
Exemplo n.º 10
0
        /// <summary>
        /// 执行指定事务的命令,指定参数,返回DataSet.
        /// </summary>
        /// <remarks>
        /// 示例:  
        ///  DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new DB2Parameter("@prodid", 24));
        /// </remarks>
        /// <param name="transaction">事务</param>
        /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
        /// <param name="commandText">存储过程名或T-SQL语句</param>
        /// <param name="commandParameters">DB2Paramter参数数组</param>
        /// <returns>返回一个包含结果集的DataSet</returns>
        public static DataSet ExecuteDataset(DB2Transaction transaction, CommandType commandType, string commandText, params DB2Parameter[] commandParameters)
        {
            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");

            // 预处理
            DB2Command cmd = new DB2Command();
            bool mustCloseConnection = false;
            PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);

            // 创建 DataAdapter & DataSet
            using (DB2DataAdapter da = new DB2DataAdapter(cmd))
            {
                DataSet ds = new DataSet();
                da.Fill(ds);
                cmd.Parameters.Clear();
                return ds;
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// 预处理用户提供的命令,数据库连接/事务/命令类型/参数
        /// </summary>
        /// <param name="command">要处理的db2Command</param>
        /// <param name="connection">数据库连接</param>
        /// <param name="transaction">一个有效的事务或者是null值</param>
        /// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param>
        /// <param name="commandText">存储过程名或都T-SQL命令文本</param>
        /// <param name="commandParameters">和命令相关联的db2Parameter参数数组,如果没有参数为'null'</param>
        /// <param name="mustCloseConnection"><c>true</c> 如果连接是打开的,则为true,其它情况下为false.</param>
        private static void PrepareCommand(DB2Command command, DB2Connection connection, DB2Transaction transaction, CommandType commandType, string commandText, DB2Parameter[] commandParameters, out bool mustCloseConnection)
        {
            if (command == null) throw new ArgumentNullException("command");
            if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");

            // If the provided connection is not open, we will open it
            if (connection.State != ConnectionState.Open)
            {
                mustCloseConnection = true;
                connection.Open();
            }
            else
            {
                mustCloseConnection = false;
            }

            // 给命令分配一个数据库连接.
            command.Connection = connection;

            // 设置命令文本(存储过程名或SQL语句)
            command.CommandText = commandText;

            // 分配事务
            if (transaction != null)
            {
                if (transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
                command.Transaction = transaction;
            }

            // 设置命令类型.
            command.CommandType = commandType;

            // 分配命令参数
            if (commandParameters != null)
            {
                AttachParameters(command, commandParameters);
            }
            return;
        }
Exemplo n.º 12
0
        /// <summary>トランザクションのコミット</summary>
        /// <remarks>必要に応じて利用する。</remarks>
        public override void CommitTransaction()
        {
            // Txオブジェクトの存在チェック
            if (this._tx == null)
            {
                // nullのためなにもしない。
            }
            else
            {
                // トランザクションのコミット
                this._tx.Commit();

                // nullクリア
                this._tx = null;
            }
        }
Exemplo n.º 13
0
        /// <summary>トランザクション開始</summary>
        /// <param name="iso">
        /// 分離レベル
        ///  1. NoTransaction:トランザクションを開始しない。
        ///  2. DefaultTransaction:規定の分離レベルでトランザクションを開始する。
        ///  3. ReadUncommitted:非コミット読み取りの分離レベルでトランザクションを開始する。
        ///  4. ReadCommitted:コミット済み読み取りの分離レベルでトランザクションを開始する。
        ///  5. RepeatableRead:反復可能読み取りの分離レベルでトランザクションを開始する。
        ///  6. Serializable:直列化可能の分離レベルでトランザクションを開始する。
        ///  7. Snapshot:エラー(DB2.NETでは無効な分離レベル)
        /// </param>
        /// <remarks>
        /// DB2は、SQL Serverと同様にロック法方式のDBMSであるため、方利用方法は、SQLServerとほぼ同様
        /// 本メソッドの実装も、スナップ ショット分離レベルの実装を除き、SQLServerと同様とした。
        /// 詳細は、DB2のマニュアルなどを参照のこと。
        /// 必要に応じて利用する。
        /// </remarks>
        public override void BeginTransaction(DbEnum.IsolationLevelEnum iso)
        {
            // 分離レベル設定のチェック
            if (iso == DbEnum.IsolationLevelEnum.NoTransaction)
            {
                // トランザクションを開始しない(nullのまま)。
            }
            else if (iso == DbEnum.IsolationLevelEnum.DefaultTransaction)
            {
                // 規定の分離レベルでトランザクションを開始する。
                this._tx = this._cnn.BeginTransaction();
            }
            else if (iso == DbEnum.IsolationLevelEnum.ReadUncommitted)
            {
                // 非コミット読み取りの分離レベルでトランザクションを開始する。
                this._tx = this._cnn.BeginTransaction(IsolationLevel.ReadUncommitted);
            }
            else if (iso == DbEnum.IsolationLevelEnum.ReadCommitted)
            {
                // コミット済み読み取りの分離レベルでトランザクションを開始する。
                this._tx = this._cnn.BeginTransaction(IsolationLevel.ReadCommitted);
            }
            else if (iso == DbEnum.IsolationLevelEnum.RepeatableRead)
            {
                // 反復可能読み取りの分離レベルでトランザクションを開始する。
                this._tx = this._cnn.BeginTransaction(IsolationLevel.RepeatableRead);
            }
            else if (iso == DbEnum.IsolationLevelEnum.Serializable)
            {
                // 直列化可能の分離レベルでトランザクションを開始する。
                this._tx = this._cnn.BeginTransaction(IsolationLevel.Serializable);
            }
            else if (iso == DbEnum.IsolationLevelEnum.Snapshot)
            {
                // 無効な分離レベル(スナップ ショット分離レベル)。
                throw new ArgumentException(String.Format(
                    PublicExceptionMessage.DB_ISO_LEVEL_PARAM_ERROR_SS, "DB2"));
            }
            else if (iso == DbEnum.IsolationLevelEnum.User)
            {
                // 無効な分離レベル(ユーザ指定)。
                throw new ArgumentException(
                    PublicExceptionMessage.DB_ISO_LEVEL_PARAM_ERROR_USR);
            }
            else if (iso == DbEnum.IsolationLevelEnum.NotConnect)
            {
                // 2009/03/29 -- 追加したNotConnectの対応(このコードブロック)。

                // 無効な分離レベル(NotConnect指定)。
                throw new ArgumentException(
                    PublicExceptionMessage.DB_ISO_LEVEL_PARAM_ERROR_NC);
            }
            else
            {
                // 通らない予定
            }

            // 分離レベル(iso)をメンバ変数に保存
            _iso = iso;
        }
Exemplo n.º 14
0
        public void ExecuteNonQueryInternal(CommandBehavior behavior)
        {
            short sqlRet;

            if (prepared && binded)
            {
                sqlRet = DB2CLIWrapper.SQLExecute(hwndStmt);
                DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_STMT, hwndStmt, "SQLExecute error.", db2Conn);
                return;
            }

            if ((db2Conn == null) || (db2Conn.State != ConnectionState.Open))
            {
                throw new InvalidOperationException("Prepare needs an open connection");
            }
            if ((refDataReader != null) &&
                (refDataReader.IsAlive))
            {
                throw new InvalidOperationException("There is already an open DataReader associated with this Connection which must be closed first.");
            }
            DB2Transaction connectionTransaction = null;

            if (db2Conn.WeakRefTransaction != null)
            {
                connectionTransaction = (DB2Transaction)db2Conn.WeakRefTransaction.Target;
            }
            if (!Object.ReferenceEquals(connectionTransaction, Transaction))
            {
                if (Transaction == null)
                {
                    throw new InvalidOperationException("A transaction was started in the connection, but the command doesn't specify a transaction");
                }
                throw new InvalidOperationException("The transaction specified at the connection doesn't belong to the connection");
            }

            if (hwndStmt == IntPtr.Zero)
            {
                AllocateStatement("InternalExecuteNonQuery");
                previousBehavior = 0;
            }
            if (previousBehavior != behavior)
            {
                if (((previousBehavior ^ behavior) & CommandBehavior.SchemaOnly) != 0)
                {
                    sqlRet = DB2CLIWrapper.SQLSetStmtAttr(hwndStmt, DB2Constants.SQL_ATTR_DEFERRED_PREPARE,
                                                          new IntPtr((behavior & CommandBehavior.SchemaOnly) != 0 ? 0 : 1), 0);
                    // TODO: don't check. what if it is not supported???
                    DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_STMT, hwndStmt, "Defered prepare.", db2Conn);

                    previousBehavior = (previousBehavior & ~CommandBehavior.SchemaOnly) | (behavior & CommandBehavior.SchemaOnly);
                }
                if (((previousBehavior ^ behavior) & CommandBehavior.SingleRow) != 0)
                {
                    sqlRet = DB2CLIWrapper.SQLSetStmtAttr(hwndStmt, DB2Constants.SQL_ATTR_MAX_ROWS,
                                                          new IntPtr((behavior & CommandBehavior.SingleRow) == 0 ? 0 : 1), 0);
                    // TODO: don't check. what if it is not supported???
                    DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_STMT, hwndStmt, "Set max rows", db2Conn);

                    previousBehavior = (previousBehavior & ~CommandBehavior.SingleRow) | (behavior & CommandBehavior.SingleRow);
                }
                previousBehavior = behavior;
            }
            if ((Transaction == null) &&
                !db2Conn.openConnection.autoCommit)
            {
                sqlRet = DB2CLIWrapper.SQLSetConnectAttr(db2Conn.DBHandle, DB2Constants.SQL_ATTR_AUTOCOMMIT, new IntPtr(DB2Constants.SQL_AUTOCOMMIT_ON), 0);
                DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, db2Conn.DBHandle, "Error setting AUTOCOMMIT ON in transaction CTOR.", db2Conn);
                db2Conn.openConnection.autoCommit = true;

                sqlRet = DB2CLIWrapper.SQLSetConnectAttr(db2Conn.DBHandle, DB2Constants.SQL_ATTR_TXN_ISOLATION, new IntPtr(DB2Constants.SQL_TXN_READ_COMMITTED), 0);
                DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, db2Conn.DBHandle, "Error setting isolation level.", db2Conn);
            }


            if ((commandText == null) || (commandText.Length == 0))
            {
                throw new InvalidOperationException("Command string is empty");
            }

            if (CommandType.StoredProcedure == commandType && !commandText.StartsWith("CALL "))
            {
                commandText = "CALL " + commandText + " ()";
            }

            if ((behavior & CommandBehavior.SchemaOnly) != 0)
            {
                if (!prepared)
                {
                    Prepare();
                }
            }
            else
            {
                if (statementParametersMemory != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(statementParametersMemory);
                    statementParametersMemory = IntPtr.Zero;
                }

                BindParams();

                if (prepared)
                {
                    sqlRet = DB2CLIWrapper.SQLExecute(hwndStmt);
                    DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_STMT, hwndStmt, "SQLExecute error.", db2Conn);
                }
                else
                {
                    sqlRet = DB2CLIWrapper.SQLExecDirect(hwndStmt, commandText, commandText.Length);
                    DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_STMT, hwndStmt, "SQLExecDirect error.", db2Conn);
                }
                statementOpen = true;

                parameters.GetOutValues();

                if (statementParametersMemory != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(statementParametersMemory);
                    statementParametersMemory = IntPtr.Zero;
                }
            }
        }
Exemplo n.º 15
0
		internal void ConnectionClosed()
		{
			DB2DataReader reader = null;
			if((refDataReader != null) && refDataReader.IsAlive)
			{
				reader = (DB2DataReader)refDataReader.Target;
			}
			if((reader != null) && refDataReader.IsAlive)
			{
				reader.Dispose();
				refDataReader = null;
			}
			CloseStatementHandle(true);

			db2Trans = null;
		}
Exemplo n.º 16
0
        /// <summary>
        /// 执行指定数据库连接对象的数据阅读器.
        /// </summary>
        /// <remarks>
        /// 如果是DB2Helper打开连接,当连接关闭DataReader也将关闭.
        /// 如果是调用都打开连接,DataReader由调用都管理.
        /// </remarks>
        /// <param name="connection">一个有效的数据库连接对象</param>
        /// <param name="transaction">一个有效的事务,或者为 'null'</param>
        /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
        /// <param name="commandText">存储过程名或T-SQL语句</param>
        /// <param name="commandParameters">DB2Parameters参数数组,如果没有参数则为'null'</param>
        /// <param name="connectionOwnership">标识数据库连接对象是由调用者提供还是由DB2Helper提供</param>
        /// <returns>返回包含结果集的DB2DataReader</returns>
        private static DB2DataReader ExecuteReader(DB2Connection connection, DB2Transaction transaction, CommandType commandType, string commandText, DB2Parameter[] commandParameters, DB2ConnectionOwnership connectionOwnership)
        {
            if (connection == null) throw new ArgumentNullException("connection");

            bool mustCloseConnection = false;
            // 创建命令
            DB2Command cmd = new DB2Command();
            try
            {
                PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);

                // 创建数据阅读器
                DB2DataReader dataReader;

                if (connectionOwnership == DB2ConnectionOwnership.External)
                {
                    dataReader = cmd.ExecuteReader();
                }
                else
                {
                    dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                }

                // 清除参数,以便再次使用..
                // HACK: There is a problem here, the output parameter values are fletched
                // when the reader is closed, so if the parameters are detached from the command
                // then the DB2Reader can磘 set its values.
                // When this happen, the parameters can磘 be used again in other command.
                bool canClear = true;
                foreach (DB2Parameter commandParameter in cmd.Parameters)
                {
                    if (commandParameter.Direction != ParameterDirection.Input)
                        canClear = false;
                }

                if (canClear)
                {
                    cmd.Parameters.Clear();
                }

                return dataReader;
            }
            catch
            {
                if (mustCloseConnection)
                    connection.Close();
                throw;
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// [私有方法][内部调用]执行指定数据库连接对象/事务的命令,映射数据表并填充数据集,DataSet/TableNames/DB2Parameters.
        /// </summary>
        /// <remarks>
        /// 示例:  
        ///  FillDataset(conn, trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new DB2Parameter("@prodid", 24));
        /// </remarks>
        /// <param name="connection">一个有效的数据库连接对象</param>
        /// <param name="transaction">一个有效的连接事务</param>
        /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
        /// <param name="commandText">存储过程名称或T-SQL语句</param>
        /// <param name="dataSet">要填充结果集的DataSet实例</param>
        /// <param name="tableNames">表映射的数据表数组
        /// 用户定义的表名 (可有是实际的表名.)
        /// </param>
        /// <param name="commandParameters">分配给命令的DB2Paramter参数数组</param>
        private static void FillDataset(DB2Connection connection, DB2Transaction transaction, CommandType commandType,
            string commandText, DataSet dataSet, string[] tableNames,
            params DB2Parameter[] commandParameters)
        {
            if (connection == null) throw new ArgumentNullException("connection");
            if (dataSet == null) throw new ArgumentNullException("dataSet");

            // 创建DB2Command命令,并进行预处理
            DB2Command command = new DB2Command();
            bool mustCloseConnection = false;
            PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);

            // 执行命令
            using (DB2DataAdapter dataAdapter = new DB2DataAdapter(command))
            {

                // 追加表映射
                if (tableNames != null && tableNames.Length > 0)
                {
                    string tableName = "Table";
                    for (int index = 0; index < tableNames.Length; index++)
                    {
                        if (tableNames[index] == null || tableNames[index].Length == 0) throw new ArgumentException("The tableNames parameter must contain a list of tables, a value was provided as null or empty string.", "tableNames");
                        dataAdapter.TableMappings.Add(tableName, tableNames[index]);
                        tableName += (index + 1).ToString();
                    }
                }

                // 填充数据集使用默认表名称
                dataAdapter.Fill(dataSet);

                // 清除参数,以便再次使用.
                command.Parameters.Clear();
            }

            if (mustCloseConnection)
                connection.Close();
        }
Exemplo n.º 18
0
        /// <summary>トランザクションのロールバック</summary>
        /// <remarks>必要に応じて利用する。</remarks>
        public override void RollbackTransaction()
        {
            // Txオブジェクトの存在チェック
            if (this._tx == null)
            {
                // nullのためなにもしない。
            }
            else
            {
                // トランザクションのロールバック
                this._tx.Rollback();

                // nullクリア
                this._tx = null;
            }
        }
Exemplo n.º 19
0
 /// <summary>
 /// 执行指定事务的命令,返回DataSet.
 /// </summary>
 /// <remarks>
 /// 示例:  
 ///  DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders");
 /// </remarks>
 /// <param name="transaction">事务</param>
 /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 /// <param name="commandText">存储过程名或T-SQL语句</param>
 /// <returns>返回一个包含结果集的DataSet</returns>
 public static DataSet ExecuteDataset(DB2Transaction transaction, CommandType commandType, string commandText)
 {
     return ExecuteDataset(transaction, commandType, commandText, (DB2Parameter[])null);
 }
Exemplo n.º 20
0
		public DB2Transaction BeginTransaction(IsolationLevel isolationL)
		{
			if ((refTransaction != null) && (refTransaction.IsAlive))
				throw new InvalidOperationException("Cannot open another transaction");
			if(State != ConnectionState.Open)
				throw new InvalidOperationException("BeginTransaction needs an open connection");

			if(refTransaction != null)
			{
				if(refTransaction.IsAlive)
					throw new InvalidOperationException("Parallel transactions not supported");

				openConnection.RollbackDeadTransaction();
				refTransaction = null;
			}
			openConnection.transactionOpen = true;
			DB2Transaction tran = new DB2Transaction(this, isolationL);
			refTransaction = new WeakReference(tran);
			return tran;
		}
Exemplo n.º 21
0
 /// <summary>
 /// 执行带事务的DB2Command.
 /// </summary>
 /// <remarks>
 /// 示例.:  
 ///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders");
 /// </remarks>
 /// <param name="transaction">一个有效的数据库连接对象</param>
 /// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param>
 /// <param name="commandText">存储过程名称或T-SQL语句</param>
 /// <returns>返回影响的行数/returns>
 public static int ExecuteNonQuery(DB2Transaction transaction, CommandType commandType, string commandText)
 {
     return ExecuteNonQuery(transaction, commandType, commandText, (DB2Parameter[])null);
 }
Exemplo n.º 22
0
 /// <summary>
 /// 执行指定数据库事务的命令,返回结果集中的第一行第一列.
 /// </summary>
 /// <remarks>
 /// 示例:  
 ///  int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount");
 /// </remarks>
 /// <param name="transaction">一个有效的连接事务</param>
 /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 /// <param name="commandText">存储过程名称或T-SQL语句</param>
 /// <returns>返回结果集中的第一行第一列</returns>
 public static object ExecuteScalar(DB2Transaction transaction, CommandType commandType, string commandText)
 {
     // 执行参数为空的方法
     return ExecuteScalar(transaction, commandType, commandText, (DB2Parameter[])null);
 }
Exemplo n.º 23
0
        /// <summary>
        /// 执行带事务的DB2Command(指定参数值).
        /// </summary>
        /// <remarks>
        /// 此方法不提供访问存储过程输出参数和返回值
        /// 示例:  
        ///  int result = ExecuteNonQuery(conn, trans, "PublishOrders", 24, 36);
        /// </remarks>
        /// <param name="transaction">一个有效的数据库连接对象</param>
        /// <param name="spName">存储过程名</param>
        /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
        /// <returns>返回受影响的行数</returns>
        public static int ExecuteNonQuery(DB2Transaction transaction, string spName, params object[] parameterValues)
        {
            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");
            if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

            // 如果有参数值
            if ((parameterValues != null) && (parameterValues.Length > 0))
            {
                // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
                DB2Parameter[] commandParameters = DB2HelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

                // 给存储过程参数赋值
                AssignParameterValues(commandParameters, parameterValues);

                // 调用重载方法
                return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
            }
            else
            {
                // 没有参数值
                return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
            }
        }
Exemplo n.º 24
0
        /// <summary>
        /// 执行指定数据库事务的命令,指定参数值,返回结果集中的第一行第一列.
        /// </summary>
        /// <remarks>
        /// 此方法不提供访问存储过程输出参数和返回值参数.
        /// 
        /// 示例:  
        ///  int orderCount = (int)ExecuteScalar(trans, "GetOrderCount", 24, 36);
        /// </remarks>
        /// <param name="transaction">一个有效的连接事务</param>
        /// <param name="spName">存储过程名称</param>
        /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
        /// <returns>返回结果集中的第一行第一列</returns>
        public static object ExecuteScalar(DB2Transaction transaction, string spName, params object[] parameterValues)
        {
            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");
            if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

            // 如果有参数值
            if ((parameterValues != null) && (parameterValues.Length > 0))
            {
                // PPull the parameters for this stored procedure from the parameter cache ()
                DB2Parameter[] commandParameters = DB2HelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

                // 给存储过程参数赋值
                AssignParameterValues(commandParameters, parameterValues);

                // 调用重载方法
                return ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters);
            }
            else
            {
                // 没有参数值
                return ExecuteScalar(transaction, CommandType.StoredProcedure, spName);
            }
        }
Exemplo n.º 25
0
 /// <summary>
 /// [调用者方式]执行指定数据库事务的数据阅读器,指定参数值.
 /// </summary>
 /// <remarks>
 /// 示例:  
 ///  DB2DataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders");
 /// </remarks>
 /// <param name="transaction">一个有效的连接事务</param>
 /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 /// <param name="commandText">存储过程名称或T-SQL语句</param>
 /// <returns>返回包含结果集的DB2DataReader</returns>
 public static DB2DataReader ExecuteReader(DB2Transaction transaction, CommandType commandType, string commandText)
 {
     return ExecuteReader(transaction, commandType, commandText, (DB2Parameter[])null);
 }
Exemplo n.º 26
0
 /// <summary>
 /// 执行指定数据库事务的DB2Command命令,并产生一个XmlReader对象做为结果集返回.
 /// </summary>
 /// <remarks>
 /// 示例:  
 ///  XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders");
 /// </remarks>
 /// <param name="transaction">一个有效的连接事务</param>
 /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 /// <param name="commandText">存储过程名称或T-SQL语句 using "FOR XML AUTO"</param>
 /// <returns>返回XmlReader结果集对象.</returns>
 public static XmlReader ExecuteXmlReader(DB2Transaction transaction, CommandType commandType, string commandText)
 {
     // 执行参数为空的方法
     return ExecuteXmlReader(transaction, commandType, commandText, (DB2Parameter[])null);
 }
Exemplo n.º 27
0
        /// <summary>
        /// [调用者方式]执行指定数据库事务的数据阅读器,指定参数值.
        /// </summary>
        /// <remarks>
        /// 此方法不提供访问存储过程输出参数和返回值参数.
        /// 
        /// 示例:  
        ///  DB2DataReader dr = ExecuteReader(trans, "GetOrders", 24, 36);
        /// </remarks>
        /// <param name="transaction">一个有效的连接事务</param>
        /// <param name="spName">存储过程名称</param>
        /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
        /// <returns>返回包含结果集的DB2DataReader</returns>
        public static DB2DataReader ExecuteReader(DB2Transaction transaction, string spName, params object[] parameterValues)
        {
            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");
            if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

            // 如果有参数值
            if ((parameterValues != null) && (parameterValues.Length > 0))
            {
                DB2Parameter[] commandParameters = DB2HelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

                AssignParameterValues(commandParameters, parameterValues);

                return ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters);
            }
            else
            {
                // 没有参数值
                return ExecuteReader(transaction, CommandType.StoredProcedure, spName);
            }
        }
Exemplo n.º 28
0
 /// <summary>
 /// 执行指定数据库事务的命令,映射数据表并填充数据集.
 /// </summary>
 /// <remarks>
 /// 示例:  
 ///  FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});
 /// </remarks>
 /// <param name="transaction">一个有效的连接事务</param>
 /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 /// <param name="commandText">存储过程名称或T-SQL语句</param>
 /// <param name="dataSet">要填充结果集的DataSet实例</param>
 /// <param name="tableNames">表映射的数据表数组
 /// 用户定义的表名 (可有是实际的表名.)
 /// </param>
 public static void FillDataset(DB2Transaction transaction, CommandType commandType,
     string commandText,
     DataSet dataSet, string[] tableNames)
 {
     FillDataset(transaction, commandType, commandText, dataSet, tableNames, null);
 }