Esempio n. 1
0
        internal Exception HandleErrorNoThrow(OdbcHandle hrHandle, ODBC32.RetCode retcode)
        {
            switch (retcode)
            {
            case ODBC32.RetCode.SUCCESS:
                break;

            case ODBC32.RetCode.SUCCESS_WITH_INFO:
                if (this.infoMessageEventHandler != null)
                {
                    OdbcErrorCollection errors = ODBC32.GetDiagErrors(null, hrHandle, retcode);
                    errors.SetSource(this.Driver);
                    this.OnInfoMessage(new OdbcInfoMessageEventArgs(errors));
                }
                break;

            default:
            {
                OdbcException innerException = OdbcException.CreateException(ODBC32.GetDiagErrors(null, hrHandle, retcode), retcode);
                if (innerException != null)
                {
                    innerException.Errors.SetSource(this.Driver);
                }
                this.ConnectionIsAlive(innerException);
                return(innerException);
            }
            }
            return(null);
        }
Esempio n. 2
0
        // non-throwing HandleError
        internal Exception HandleErrorNoThrow(OdbcHandle hrHandle, ODBC32.RetCode retcode)
        {
            Debug.Assert(retcode != ODBC32.RetCode.INVALID_HANDLE, "retcode must never be ODBC32.RetCode.INVALID_HANDLE");

            switch (retcode)
            {
            case ODBC32.RetCode.SUCCESS:
                break;

            case ODBC32.RetCode.SUCCESS_WITH_INFO:
            {
                //Optimize to only create the event objects and obtain error info if
                //the user is really interested in retriveing the events...
                if (_infoMessageEventHandler != null)
                {
                    OdbcErrorCollection errors = ODBC32.GetDiagErrors(null, hrHandle, retcode);
                    errors.SetSource(this.Driver);
                    OnInfoMessage(new OdbcInfoMessageEventArgs(errors));
                }
                break;
            }

            default:
                OdbcException e = OdbcException.CreateException(ODBC32.GetDiagErrors(null, hrHandle, retcode), retcode);
                if (e != null)
                {
                    e.Errors.SetSource(this.Driver);
                }
                ConnectionIsAlive(e);            // this will close and throw if the connection is dead
                return((Exception)e);
            }
            return(null);
        }
Esempio n. 3
0
        ODBC32.RETCODE _retcode;    // DO NOT REMOVE! only needed for serialization purposes, because Everett had it.

        static internal OdbcException CreateException(OdbcErrorCollection errors, ODBC32.RetCode retcode) {
            StringBuilder builder = new StringBuilder();
            foreach (OdbcError error in errors) {
                if (builder.Length > 0) {
                    builder.Append(Environment.NewLine);
                }

                builder.Append(Res.GetString(Res.Odbc_ExceptionMessage, ODBC32.RetcodeToString(retcode), error.SQLState, error.Message)); // MDAC 68337
            }
            OdbcException exception = new OdbcException(builder.ToString(), errors);
            return exception;
        }
Esempio n. 4
0
 /// <summary>
 /// Logs ODBC Exceptions in a text file
 /// </summary>
 /// <param name="odbcException">The ODBC exception that has to be logged</param>
 /// <param name="query">The location of the logfile (including filename)</param>
 public static void LogOdbcException(OdbcException odbcException, string query)
 {
     StreamWriter logger = new StreamWriter("DBFaults.txt", true); //Open the file and get ready to append the error-info to the file.
     logger.WriteLine(odbcException.Message);
     logger.WriteLine(odbcException.Errors[0].Message);
     logger.WriteLine(odbcException.Errors[0].NativeError);
     logger.WriteLine(odbcException.Errors[0].SQLState);
     logger.WriteLine(query);
     logger.WriteLine(DateTime.Now); //Put a timestamp so we know when the error happened
     logger.WriteLine("----------++++++++++----------"); //Make it clear this is the end of the error.
     logger.Flush();
     logger.Close();
 }
Esempio n. 5
0
        internal static OdbcException CreateException(OdbcErrorCollection errors, ODBC32.RetCode retcode)
        {
            StringBuilder builder = new StringBuilder();

            foreach (OdbcError error in errors)
            {
                if (builder.Length > 0)
                {
                    builder.Append(Environment.NewLine);
                }

                builder.Append(SR.GetString(SR.Odbc_ExceptionMessage, ODBC32.RetcodeToString(retcode), error.SQLState, error.Message)); // MDAC 68337
            }
            OdbcException exception = new OdbcException(builder.ToString(), errors);

            return(exception);
        }
    //
    // WriteToEventLog
    //   A helper function that writes exception detail to the event log. Exceptions
    // are written to the event log as a security measure to avoid private database
    // details from being returned to the browser. If a method does not return a status
    // or boolean indicating the action succeeded or failed, a generic exception is also
    // thrown by the caller.
    //
    private void WriteToEventLog(OdbcException e, string action)
    {
        EventLog log = new EventLog();
            log.Source = eventSource;
            log.Log = eventLog;

            string message = exceptionMessage + "\n\n";
            message += "Action: " + action + "\n\n";
            message += "Exception: " + e.ToString();

            log.WriteEntry(message);
    }
Esempio n. 7
0
 private void handleException(OdbcException MyOdbcException)
 {
     for (int i=0; i < MyOdbcException.Errors.Count; i++)
     {
         Console.Write("SQL ERROR #" + i + "\n" +
             "Message: " + MyOdbcException.Errors[i].Message + "\n" +
             "Native: " + MyOdbcException.Errors[i].NativeError.ToString() + "\n" +
             "Source: " + MyOdbcException.Errors[i].Source + "\n" +
             "SQL: " + MyOdbcException.Errors[i].SQLState + "\n");
     }
 }
Esempio n. 8
0
 private void MessageHandler(OdbcException e)
 {
     OnOdbcInfoMessage(CreateOdbcInfoMessageEvent(e.Errors));
 }
Esempio n. 9
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;
        }
Esempio n. 10
0
        long GetBytes(int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
        {
            if (IsClosed)
            {
                throw new InvalidOperationException("Reader is not open.");
            }
            if (currentRow == -1)
            {
                throw new InvalidOperationException("No data available.");
            }

            OdbcReturn ret = OdbcReturn.Error;
            bool       copyBuffer = false;
            int        returnVal = 0, outsize = 0;

            byte [] tbuff = new byte [length + 1];

            if (buffer == null)
            {
                length = 0;
            }
            ret = libodbc.SQLGetData(hstmt, (ushort)(i + 1), SQL_C_TYPE.BINARY, tbuff, length,
                                     ref outsize);

            if (ret == OdbcReturn.NoData)
            {
                return(0);
            }

            if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
            {
                throw Connection.CreateOdbcException(OdbcHandleType.Stmt, hstmt);
            }

            OdbcException odbcException = null;

            if ((ret == OdbcReturn.SuccessWithInfo))
            {
                odbcException = Connection.CreateOdbcException(
                    OdbcHandleType.Stmt, hstmt);
            }

            if (buffer == null)
            {
                return(outsize);                //if buffer is null,return length of the field
            }
            if (ret == OdbcReturn.SuccessWithInfo)
            {
                if (outsize == (int)OdbcLengthIndicator.NoTotal)
                {
                    copyBuffer = true;
                }
                else if (outsize == (int)OdbcLengthIndicator.NullData)
                {
                    copyBuffer = false;
                    returnVal  = -1;
                }
                else
                {
                    string sqlstate = odbcException.Errors [0].SQLState;
                    //SQLState: String Data, Right truncated
                    if (sqlstate != libodbc.SQLSTATE_RIGHT_TRUNC)
                    {
                        throw odbcException;
                    }
                    copyBuffer = true;
                }
            }
            else
            {
                copyBuffer = outsize == -1 ? false : true;
                returnVal  = outsize;
            }

            if (copyBuffer)
            {
                if (outsize == (int)OdbcLengthIndicator.NoTotal)
                {
                    int j = 0;
                    while (tbuff [j] != libodbc.C_NULL)
                    {
                        buffer [bufferIndex + j] = tbuff [j];
                        j++;
                    }
                    returnVal = j;
                }
                else
                {
                    int read_bytes = Math.Min(outsize, length);
                    for (int j = 0; j < read_bytes; j++)
                    {
                        buffer [bufferIndex + j] = tbuff [j];
                    }
                    returnVal = read_bytes;
                }
            }
            return(returnVal);
        }
Esempio n. 11
0
		private void MessageHandler (OdbcException e)
		{
			OnOdbcInfoMessage (CreateOdbcInfoMessageEvent (e.Errors));
		}
Esempio n. 12
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;
		}
Esempio n. 13
0
 public static String VypisVyjimky(OdbcException e)
 {
     String errorMessages = "";
     String vypis;
     for (int i = 0; i < e.Errors.Count; i++)
     {
         errorMessages = "Index #" + i + "\n" +
                          "Message: " + e.Errors[i].Message + "\n" +
                          "NativeError: " + e.Errors[i].NativeError.ToString() + "\n" +
                          "Source: " + e.Errors[i].Source + "\n" +
                          "SQL: " + e.Errors[i].SQLState + "\n";
     }
     vypis = "<BR>" + errorMessages + "<BR>";
     return vypis;
 }