internal static void GetDiagErrors(OdbcErrorCollection errors, string source, OdbcHandle hrHandle, RetCode retcode)
 {
     if (retcode != RetCode.SUCCESS)
     {
         short record = 0;
         short cchActual = 0;
         StringBuilder message = new StringBuilder(0x400);
         bool flag = true;
         while (flag)
         {
             int num3;
             string str;
             record = (short) (record + 1);
             retcode = hrHandle.GetDiagnosticRecord(record, out str, message, out num3, out cchActual);
             if ((RetCode.SUCCESS_WITH_INFO == retcode) && ((message.Capacity - 1) < cchActual))
             {
                 message.Capacity = cchActual + 1;
                 retcode = hrHandle.GetDiagnosticRecord(record, out str, message, out num3, out cchActual);
             }
             if ((retcode == RetCode.SUCCESS) || (retcode == RetCode.SUCCESS_WITH_INFO))
             {
                 errors.Add(new OdbcError(source, message.ToString(), str, num3));
             }
         }
     }
 }
 internal static void GetDiagErrors(OdbcErrorCollection errors, string source, OdbcHandle hrHandle, RetCode retcode)
 {
     if (retcode != RetCode.SUCCESS)
     {
         short         record    = 0;
         short         cchActual = 0;
         StringBuilder message   = new StringBuilder(0x400);
         bool          flag      = true;
         while (flag)
         {
             int    num3;
             string str;
             record  = (short)(record + 1);
             retcode = hrHandle.GetDiagnosticRecord(record, out str, message, out num3, out cchActual);
             if ((RetCode.SUCCESS_WITH_INFO == retcode) && ((message.Capacity - 1) < cchActual))
             {
                 message.Capacity = cchActual + 1;
                 retcode          = hrHandle.GetDiagnosticRecord(record, out str, message, out num3, out cchActual);
             }
             if ((retcode == RetCode.SUCCESS) || (retcode == RetCode.SUCCESS_WITH_INFO))
             {
                 errors.Add(new OdbcError(source, message.ToString(), str, num3));
             }
         }
     }
 }
示例#3
0
        // Helpers
        public static OdbcErrorCollection   GetDiagErrors(string source, HandleRef hrHandle, SQL_HANDLE hType, RETCODE retcode)
        {
            switch (retcode)
            {
            case RETCODE.SUCCESS:
                return(null);

            case RETCODE.INVALID_HANDLE:
                throw ODC.InvalidHandle();

            default:
            {
                Int32 NativeError;
                Int16 iRec      = 0;
                Int16 cchActual = 0;

                OdbcErrorCollection errors = new OdbcErrorCollection();
                try {
                    using (CNativeBuffer message = new CNativeBuffer(1024)) {
                        using (CNativeBuffer state = new CNativeBuffer(12)) {
                            bool moreerrors = true;
                            while (moreerrors)
                            {
                                retcode = (RETCODE)UnsafeNativeMethods.Odbc32.SQLGetDiagRecW(
                                    (short)hType,
                                    hrHandle,
                                    ++iRec,        //Orindals are 1:base in odbc
                                    state,
                                    out NativeError,
                                    message,
                                    (short)(message.Length / 2),       //cch
                                    out cchActual);                    //cch

                                //Note: SUCCESS_WITH_INFO from SQLGetDiagRec would be because
                                //the buffer is not large enough for the error string.
                                moreerrors = (retcode == RETCODE.SUCCESS || retcode == RETCODE.SUCCESS_WITH_INFO);
                                if (moreerrors)
                                {
                                    //Sets up the InnerException as well...
                                    errors.Add(new OdbcError(
                                                   source,
                                                   (string)message.MarshalToManaged(SQL_C.WCHAR, SQL_NTS),
                                                   (string)state.MarshalToManaged(SQL_C.WCHAR, SQL_NTS),
                                                   NativeError
                                                   )
                                               );
                                }
                            }
                        }
                    }
                }
                catch {
                    throw;
                }
                return(errors);
            }
            }
        }
示例#4
0
        internal OdbcException CreateOdbcException(OdbcHandleType HandleType, IntPtr Handle)
        {
            short      buflen      = 256;
            short      txtlen      = 0;
            int        nativeerror = 0;
            OdbcReturn ret         = OdbcReturn.Success;

            OdbcErrorCollection errors = new OdbcErrorCollection();

            while (true)
            {
                byte [] buf_MsgText  = new byte [buflen * 2];
                byte [] buf_SqlState = new byte [buflen * 2];

                switch (HandleType)
                {
                case OdbcHandleType.Dbc:
                    ret = libodbc.SQLError(IntPtr.Zero, Handle, IntPtr.Zero, buf_SqlState,
                                           ref nativeerror, buf_MsgText, buflen, ref txtlen);
                    break;

                case OdbcHandleType.Stmt:
                    ret = libodbc.SQLError(IntPtr.Zero, IntPtr.Zero, Handle, buf_SqlState,
                                           ref nativeerror, buf_MsgText, buflen, ref txtlen);
                    break;

                case OdbcHandleType.Env:
                    ret = libodbc.SQLError(Handle, IntPtr.Zero, IntPtr.Zero, buf_SqlState,
                                           ref nativeerror, buf_MsgText, buflen, ref txtlen);
                    break;
                }

                if (ret != OdbcReturn.Success)
                {
                    break;
                }

                string state   = RemoveTrailingNullChar(Encoding.Unicode.GetString(buf_SqlState));
                string message = Encoding.Unicode.GetString(buf_MsgText, 0, txtlen * 2);

                errors.Add(new OdbcError(message, state, nativeerror));
            }

            string source = SafeDriver;

            foreach (OdbcError error in errors)
            {
                error.SetSource(source);
            }
            return(new OdbcException(errors));
        }
示例#5
0
        internal static void GetDiagErrors(OdbcErrorCollection errors, string source, OdbcHandle hrHandle, RetCode retcode)
        {
            Debug.Assert(retcode != ODBC32.RetCode.INVALID_HANDLE, "retcode must never be ODBC32.RetCode.INVALID_HANDLE");
            if (RetCode.SUCCESS != retcode)
            {
                int   NativeError;
                short iRec      = 0;
                short cchActual = 0;

                StringBuilder message = new StringBuilder(1024);
                string        sqlState;
                bool          moreerrors = true;
                while (moreerrors)
                {
                    ++iRec;

                    retcode = hrHandle.GetDiagnosticRecord(iRec, out sqlState, message, out NativeError, out cchActual);
                    if ((RetCode.SUCCESS_WITH_INFO == retcode) && (message.Capacity - 1 < cchActual))
                    {
                        message.Capacity = cchActual + 1;
                        retcode          = hrHandle.GetDiagnosticRecord(iRec, out sqlState, message, out NativeError, out cchActual);
                    }

                    //Note: SUCCESS_WITH_INFO from SQLGetDiagRec would be because
                    //the buffer is not large enough for the error string.
                    moreerrors = (retcode == RetCode.SUCCESS || retcode == RetCode.SUCCESS_WITH_INFO);
                    if (moreerrors)
                    {
                        //Sets up the InnerException as well...
                        errors.Add(new OdbcError(
                                       source,
                                       message.ToString(),
                                       sqlState,
                                       NativeError
                                       )
                                   );
                    }
                }
            }
        }
示例#6
0
        static internal void GetDiagErrors(OdbcErrorCollection errors, string source, OdbcHandle hrHandle, RetCode retcode) {
            Debug.Assert(retcode!=ODBC32.RetCode.INVALID_HANDLE, "retcode must never be ODBC32.RetCode.INVALID_HANDLE");
            if (RetCode.SUCCESS != retcode) {
                Int32       NativeError;
                Int16       iRec            = 0;
                Int16       cchActual       = 0;

                StringBuilder message = new StringBuilder(1024);
                string sqlState;
                bool moreerrors = true;
                while(moreerrors) {

                    ++iRec;

                    retcode = hrHandle.GetDiagnosticRecord(iRec, out sqlState, message, out NativeError, out cchActual);
                    if ((RetCode.SUCCESS_WITH_INFO == retcode) && (message.Capacity-1 < cchActual)) {
                        message.Capacity = cchActual+1;
                        retcode = hrHandle.GetDiagnosticRecord(iRec, out sqlState, message, out NativeError, out cchActual);
                    }

                    //Note: SUCCESS_WITH_INFO from SQLGetDiagRec would be because
                    //the buffer is not large enough for the error string.
                    moreerrors = (retcode == RetCode.SUCCESS || retcode == RetCode.SUCCESS_WITH_INFO);
                    if(moreerrors) {
                        //Sets up the InnerException as well...
                        errors.Add(new OdbcError(
                            source,
                            message.ToString(),
                            sqlState,
                            NativeError
                            )
                        );
                    }
                }
            }
        }
		internal OdbcException(OdbcError Error) : base (Error.Message)
		{
			odbcErrors = new OdbcErrorCollection ();
			odbcErrors.Add (Error);
		}
示例#8
0
        void Open()
        {
            if (State == ConnectionState.Open)
            {
                throw new InvalidOperationException();
            }

            OdbcReturn    ret = OdbcReturn.Error;
            OdbcException e   = null;

            try {
                // allocate Environment handle
                ret = libodbc.SQLAllocHandle(OdbcHandleType.Env, IntPtr.Zero, ref henv);
                if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
                {
                    OdbcErrorCollection errors = new OdbcErrorCollection();
                    errors.Add(new OdbcError(this));
                    e = new OdbcException(errors);
                    MessageHandler(e);
                    throw e;
                }

                ret = libodbc.SQLSetEnvAttr(henv, OdbcEnv.OdbcVersion, (IntPtr)libodbc.SQL_OV_ODBC3, 0);
                if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
                {
                    throw CreateOdbcException(OdbcHandleType.Env, henv);
                }

                // allocate connection handle
                ret = libodbc.SQLAllocHandle(OdbcHandleType.Dbc, henv, ref hdbc);
                if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
                {
                    throw CreateOdbcException(OdbcHandleType.Env, henv);
                }

                // DSN connection
                if (ConnectionString.ToLower().IndexOf("dsn=") >= 0)
                {
                    string    _uid = string.Empty, _pwd = string.Empty, _dsn = string.Empty;
                    string [] items = ConnectionString.Split(new char[1] {
                        ';'
                    });
                    foreach (string item in items)
                    {
                        string [] parts = item.Split(new char[1] {
                            '='
                        });
                        switch (parts [0].Trim().ToLower())
                        {
                        case "dsn":
                            _dsn = parts [1].Trim();
                            break;

                        case "uid":
                            _uid = parts [1].Trim();
                            break;

                        case "pwd":
                            _pwd = parts [1].Trim();
                            break;
                        }
                    }
                    ret = libodbc.SQLConnect(hdbc, _dsn, -3, _uid, -3, _pwd, -3);
                    if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
                    {
                        throw CreateOdbcException(OdbcHandleType.Dbc, hdbc);
                    }
                }
                else
                {
                    // DSN-less Connection
                    string OutConnectionString = new String(' ', 1024);
                    short  OutLen = 0;
                    ret = libodbc.SQLDriverConnect(hdbc, IntPtr.Zero, ConnectionString, -3,
                                                   OutConnectionString, (short)OutConnectionString.Length, ref OutLen, 0);
                    if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
                    {
                        throw CreateOdbcException(OdbcHandleType.Dbc, hdbc);
                    }
                }

                RaiseStateChange(ConnectionState.Closed, ConnectionState.Open);
            } catch {
                // free handles if any.
                FreeHandles();
                throw;
            }
            disposed = false;
        }
示例#9
0
		internal OdbcException CreateOdbcException (OdbcHandleType HandleType, IntPtr Handle)
		{
			short buflen = 256;
			short txtlen = 0;
			int nativeerror = 0;
			OdbcReturn ret = OdbcReturn.Success;

			OdbcErrorCollection errors = new OdbcErrorCollection ();

			while (true) {
				byte [] buf_MsgText = new byte [buflen * 2];
				byte [] buf_SqlState = new byte [buflen * 2];

				switch (HandleType) {
				case OdbcHandleType.Dbc:
					ret = libodbc.SQLError (IntPtr.Zero, Handle, IntPtr.Zero, buf_SqlState,
						ref nativeerror, buf_MsgText, buflen, ref txtlen);
					break;
				case OdbcHandleType.Stmt:
					ret = libodbc.SQLError (IntPtr.Zero, IntPtr.Zero, Handle, buf_SqlState,
						ref nativeerror, buf_MsgText, buflen, ref txtlen);
					break;
				case OdbcHandleType.Env:
					ret = libodbc.SQLError (Handle, IntPtr.Zero, IntPtr.Zero, buf_SqlState,
						ref nativeerror, buf_MsgText, buflen, ref txtlen);
					break;
				}

				if (ret != OdbcReturn.Success)
					break;

				string state = RemoveTrailingNullChar (Encoding.Unicode.GetString (buf_SqlState));
				string message = Encoding.Unicode.GetString (buf_MsgText, 0, txtlen * 2);

				errors.Add (new OdbcError (SafeDriver, message, state, nativeerror));
			}

			string source = SafeDriver;
			foreach (OdbcError error in errors)
				error.SetSource (source);
			return new OdbcException (errors);
		}
示例#10
0
		void Open ()
		{
			if (State == ConnectionState.Open)
				throw new InvalidOperationException ();

			OdbcReturn ret = OdbcReturn.Error;
			OdbcException e = null;
		
			try {
				// allocate Environment handle
				ret = libodbc.SQLAllocHandle (OdbcHandleType.Env, IntPtr.Zero, ref henv);
				if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo)) {
					OdbcErrorCollection errors = new OdbcErrorCollection ();
					errors.Add (new OdbcError (SafeDriver, "Error in " + SafeDriver, "", 1));
					e = new OdbcException (errors);
					MessageHandler (e);
					throw e;
				}

				ret = libodbc.SQLSetEnvAttr (henv, OdbcEnv.OdbcVersion, (IntPtr) libodbc.SQL_OV_ODBC3 , 0); 
				if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
					throw CreateOdbcException (OdbcHandleType.Env, henv);

				// allocate connection handle
				ret = libodbc.SQLAllocHandle (OdbcHandleType.Dbc, henv, ref hdbc);
				if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
					throw CreateOdbcException (OdbcHandleType.Env, henv);

				// DSN connection
				if (ConnectionString.ToLower ().IndexOf ("dsn=") >= 0) {
					string _uid = string.Empty, _pwd = string.Empty, _dsn = string.Empty;
					string [] items = ConnectionString.Split (new char[1] {';'});
					foreach (string item in items)
					{
						string [] parts = item.Split (new char[1] {'='});
						switch (parts [0].Trim ().ToLower ()) {
						case "dsn":
							_dsn = parts [1].Trim ();
							break;
						case "uid":
							_uid = parts [1].Trim ();
							break;
						case "pwd":
							_pwd = parts [1].Trim ();
							break;
						}
					}
					ret = libodbc.SQLConnect(hdbc, _dsn, -3, _uid, -3, _pwd, -3);
					if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
						throw CreateOdbcException (OdbcHandleType.Dbc, hdbc);
				} else {
					// DSN-less Connection
					string OutConnectionString = new String (' ',1024);
					short OutLen = 0;
					ret = libodbc.SQLDriverConnect (hdbc, IntPtr.Zero, ConnectionString, -3, 
						OutConnectionString, (short) OutConnectionString.Length, ref OutLen, 0);
					if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
						throw CreateOdbcException (OdbcHandleType.Dbc, hdbc);
				}

				RaiseStateChange (ConnectionState.Closed, ConnectionState.Open);
			} catch {
				// free handles if any.
				FreeHandles ();
				throw;
			}
			disposed = false;
		}
示例#11
0
 internal OdbcException(OdbcError Error) : base(Error.Message)
 {
     odbcErrors = new OdbcErrorCollection();
     odbcErrors.Add(Error);
 }