示例#1
0
        // GetStatementHandle
        // ------------------
        // Try to return a cached statement handle.
        //
        // Creates a CmdWrapper object if necessary
        // If no handle is available a handle will be allocated.
        // Bindings will be unbound if a handle is cached and the bindings are invalid.
        //
        internal HandleRef GetStatementHandle()
        {
            ODBC32.RETCODE retcode;

            // update the command wrapper object, allocate buffer
            // create reader object
            //
            if (_cmdWrapper == null)
            {
                _cmdWrapper = new CMDWrapper();
            }
            _cmdWrapper._connection   = _connection;
            _cmdWrapper._connectionId = _connection._dbcWrapper._instanceId;
            _cmdWrapper._cmdText      = _cmdText;

            if (_cmdWrapper._dataReaderBuf == null)
            {
                _cmdWrapper._dataReaderBuf = new CNativeBuffer(4096);
            }

            // if there is already a statement handle we need to do some cleanup
            //
            if (_cmdWrapper._stmt != IntPtr.Zero)
            {
                if ((null != _parameterCollection) && (0 < _parameterCollection.Count))
                {
                    if (_parameterCollection.CollectionIsBound && _parameterCollection.BindingIsValid)
                    {
                        retcode = (ODBC32.RETCODE)UnsafeNativeMethods.Odbc32.SQLFreeStmt(
                            _cmdWrapper,
                            (short)ODBC32.STMT.UNBIND
                            );
                        // UNBIND should never fail so we assert only
                        if (retcode != ODBC32.RETCODE.SUCCESS)
                        {
                            Debug.Assert(false, "SQLFreeStmt(hstmt, (short)ODBC32.STMT.UNBIND) failed");
                        }
                        _parameterCollection.CollectionIsBound = false;
                    }
                }
            }
            else
            {
                _isPrepared = false;    // a new statement can't be prepare
                if (null != _parameterCollection)
                {
                    _parameterCollection.CollectionIsBound = false;
                }
                _cmdWrapper.AllocateStatementHandle(ref _cmdWrapper._stmt);
            }
            return(_cmdWrapper);
        }