コード例 #1
0
        private void InternalOpen(string connnectionString, DB2Connection connection)
        {
            try
            {
                DB2Constants.RetCode sqlRet = (DB2Constants.RetCode)DB2CLIWrapper.SQLAllocHandle(DB2Constants.SQL_HANDLE_DBC, DB2Environment.Instance.PenvHandle, out dbHandle);
                DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, DB2Environment.Instance.PenvHandle, "Unable to allocate database handle in DB2Connection.", connection);

                StringBuilder outConnectStr = new StringBuilder(DB2Constants.SQL_MAX_OPTION_STRING_LENGTH);
                short         numOutCharsReturned;

                sqlRet = (DB2Constants.RetCode)DB2CLIWrapper.SQLDriverConnect(dbHandle, IntPtr.Zero,
                                                                              connnectionString, DB2Constants.SQL_NTS,
                                                                              outConnectStr, DB2Constants.SQL_MAX_OPTION_STRING_LENGTH, out numOutCharsReturned,
                                                                              DB2Constants.SQL_DRIVER_NOPROMPT);

                DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, dbHandle, "Unable to connect to the database.", connection);

                databaseProductName = SQLGetInfo(dbHandle, DB2Constants.SQL_DBMS_NAME);
                databaseVersion     = SQLGetInfo(dbHandle, DB2Constants.SQL_DBMS_VER);

                /* Set the attribute SQL_ATTR_XML_DECLARATION to skip the XML declaration from XML Data */
                sqlRet = (DB2Constants.RetCode)DB2CLIWrapper.SQLSetConnectAttr(dbHandle, DB2Constants.SQL_ATTR_XML_DECLARATION, new IntPtr(DB2Constants.SQL_XML_DECLARATION_NONE), DB2Constants.SQL_NTS);
                DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, dbHandle, "Unable to set SQL_ATTR_XML_DECLARATION", connection);


                connection.NativeOpenPerformed = true;

                if ((settings.Pool == null) || (settings.Pool.databaseProductName == null))
                {
                    settings.Pool.databaseProductName = databaseProductName;
                    settings.Pool.databaseVersion     = databaseVersion;
                }
                else if (settings.Pool != null)
                {
                    if (settings.Pool != null)
                    {
                        databaseProductName = settings.Pool.databaseProductName;
                        databaseVersion     = settings.Pool.databaseVersion;
                    }
                }
            }
            catch
            {
                if (dbHandle != IntPtr.Zero)
                {
                    DB2CLIWrapper.SQLFreeHandle(DB2Constants.SQL_HANDLE_DBC, dbHandle);
                    dbHandle = IntPtr.Zero;
                }
                throw;
            }
        }
コード例 #2
0
        internal DB2Transaction(DB2Connection con, IsolationLevel isoL)
        {
            long db2IsoL;

            connection = con;
            short sqlRet;

            isolationLevel = isoL;

            switch (isoL)
            {
            default:
            case System.Data.IsolationLevel.Chaos:                                      //No DB2equivalent, default to SQL_TXN_READ_COMMITTED
            case System.Data.IsolationLevel.ReadCommitted:                              //SQL_TXN_READ_COMMITTED
                db2IsoL = DB2Constants.SQL_TXN_READ_COMMITTED;
                break;

            case System.Data.IsolationLevel.ReadUncommitted:                            //SQL_TXN_READ_UNCOMMITTED
                db2IsoL = DB2Constants.SQL_TXN_READ_UNCOMMITTED;
                break;

            case System.Data.IsolationLevel.RepeatableRead:                             //SQL_TXN_REPEATABLE_READ
                db2IsoL = DB2Constants.SQL_TXN_REPEATABLE_READ;
                break;

            case System.Data.IsolationLevel.Serializable:                               //SQL_TXN_SERIALIZABLE_READ
                db2IsoL = DB2Constants.SQL_TXN_SERIALIZABLE_READ;
                break;
            }

            //AutoCommit
            if (connection.AutoCommit)
            {
                sqlRet = DB2CLIWrapper.SQLSetConnectAttr(connection.DBHandle, DB2Constants.SQL_ATTR_AUTOCOMMIT, new IntPtr(DB2Constants.SQL_AUTOCOMMIT_OFF), 0);
                DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, connection.DBHandle, "Error setting AUTOCOMMIT OFF in transaction CTOR.", connection);
                connection.AutoCommit = false;
            }
            sqlRet = DB2CLIWrapper.SQLSetConnectAttr(connection.DBHandle, DB2Constants.SQL_ATTR_TXN_ISOLATION, new IntPtr(db2IsoL), 0);
            DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, connection.DBHandle, "Error setting isolation level.", connection);

            state = TransactionState.Open;
        }
コード例 #3
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;
                }
            }
        }