protected OdbcHandle(ODBC32.SQL_HANDLE handleType, OdbcHandle parentHandle) : base(IntPtr.Zero, true)
        {
            this._handleType = handleType;
            bool success = false;

            ODBC32.RetCode sUCCESS = ODBC32.RetCode.SUCCESS;
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                switch (handleType)
                {
                case ODBC32.SQL_HANDLE.ENV:
                    sUCCESS = UnsafeNativeMethods.SQLAllocHandle(handleType, IntPtr.Zero, out this.handle);
                    goto Label_0099;

                case ODBC32.SQL_HANDLE.DBC:
                case ODBC32.SQL_HANDLE.STMT:
                    parentHandle.DangerousAddRef(ref success);
                    sUCCESS = UnsafeNativeMethods.SQLAllocHandle(handleType, parentHandle, out this.handle);
                    goto Label_0099;
                }
            }
            finally
            {
                if (success)
                {
                    switch (handleType)
                    {
                    case ODBC32.SQL_HANDLE.DBC:
                    case ODBC32.SQL_HANDLE.STMT:
                        if (!(IntPtr.Zero != base.handle))
                        {
                            goto Label_0092;
                        }
                        this._parentHandle = parentHandle;
                        break;
                    }
                }
                goto Label_0098;
Label_0092:
                parentHandle.DangerousRelease();
                Label_0098 :;
            }
            Label_0099 :
            Bid.TraceSqlReturn("<odbc.SQLAllocHandle|API|ODBC|RET> %08X{SQLRETURN}\n", sUCCESS);
            if ((ADP.PtrZero == base.handle) || (sUCCESS != ODBC32.RetCode.SUCCESS))
            {
                throw ODBC.CantAllocateEnvironmentHandle(sUCCESS);
            }
        }
Ejemplo n.º 2
0
        protected OdbcHandle(ODBC32.SQL_HANDLE handleType, OdbcHandle parentHandle) : base(IntPtr.Zero, true)
        {
            _handleType = handleType;

            bool mustRelease = false;

            ODBC32.RetCode retcode = ODBC32.RetCode.SUCCESS;

            // using ConstrainedRegions to make the native ODBC call and AddRef the parent
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                // validate handleType
                switch (handleType)
                {
                case ODBC32.SQL_HANDLE.ENV:
                    Debug.Assert(null == parentHandle, "did not expect a parent handle");
                    retcode = Interop.Odbc.SQLAllocHandle(handleType, IntPtr.Zero, out base.handle);
                    break;

                case ODBC32.SQL_HANDLE.DBC:
                case ODBC32.SQL_HANDLE.STMT:
                    // must addref before calling native so it won't be released just after
                    Debug.Assert(null != parentHandle, "expected a parent handle");     // safehandle can't be null
                    parentHandle.DangerousAddRef(ref mustRelease);

                    retcode = Interop.Odbc.SQLAllocHandle(handleType, parentHandle, out base.handle);
                    break;

                //              case ODBC32.SQL_HANDLE.DESC:
                default:
                    Debug.Fail("unexpected handleType");
                    break;
                }
            }
            finally
            {
                if (mustRelease)
                {
                    switch (handleType)
                    {
                    case ODBC32.SQL_HANDLE.DBC:
                    case ODBC32.SQL_HANDLE.STMT:
                        if (IntPtr.Zero != base.handle)
                        {
                            // must assign _parentHandle after a handle is actually created
                            // since ReleaseHandle will only call DangerousRelease if a handle exists
                            _parentHandle = parentHandle;
                        }
                        else
                        {
                            // without a handle, ReleaseHandle may not be called
                            parentHandle.DangerousRelease();
                        }
                        break;
                    }
                }
            }

            if ((ADP.PtrZero == base.handle) || (ODBC32.RetCode.SUCCESS != retcode))
            {
                //
                throw ODBC.CantAllocateEnvironmentHandle(retcode);
            }
        }