OdbcException 類別

定義

ODBC 資料來源傳回警告或錯誤時產生的例外狀況。The exception that is generated when a warning or error is returned by an ODBC data source. 無法繼承這個類別。This class cannot be inherited.

public ref class OdbcException sealed : System::Data::Common::DbException
[System.Serializable]
public sealed class OdbcException : System.Data.Common.DbException
type OdbcException = class
    inherit DbException
Public NotInheritable Class OdbcException
Inherits DbException
繼承
屬性

範例

下列範例會產生 OdbcException,因為遺漏資料來源,然後顯示例外狀況。The following example generates an OdbcException because of a missing data source, and then displays the exception.

public void ShowOdbcException() 
{
   string mySelectQuery = "SELECT column1 FROM table1";
   OdbcConnection myConnection =
      new OdbcConnection("DRIVER={SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;");
   OdbcCommand myCommand = new OdbcCommand(mySelectQuery,myConnection);
   try 
   {
      myCommand.Connection.Open();
   }
   catch (OdbcException e) 
   {
     string errorMessages = "";

     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";
     }

     System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
     log.Source = "My Application";
     log.WriteEntry(errorMessages);
     Console.WriteLine("An exception occurred. Please contact your system administrator.");
   }
}
Public Sub ShowOdbcException()
    Dim mySelectQuery As String = "SELECT column1 FROM table1"
    Dim myConnection As New OdbcConnection _
       ("DRIVER={SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;")
    Dim myCommand As New OdbcCommand(mySelectQuery, myConnection)
    Try
        myCommand.Connection.Open()
    Catch e As OdbcException
        Dim errorMessages As String
        Dim i As Integer

        For i = 0 To e.Errors.Count - 1
            errorMessages += "Index #" & i.ToString() & ControlChars.Cr _
                           & "Message: " & e.Errors(i).Message & ControlChars.Cr _
                           & "NativeError: " & e.Errors(i).NativeError.ToString() & ControlChars.Cr _
                           & "Source: " & e.Errors(i).Source & ControlChars.Cr _
                           & "SQL: " & e.Errors(i).SQLState & ControlChars.Cr
        Next i

       Dim log As New System.Diagnostics.EventLog()
       log.Source = "My Application"
       log.WriteEntry(errorMessages)
       Console.WriteLine("An exception occurred. Please contact your system administrator.")
    End Try
End Sub

備註

每當 OdbcDataAdapter 遇到伺服器產生的錯誤時,就會建立這個類別(用戶端錯誤會引發為標準的 common language runtime 例外狀況)。This class is created whenever the OdbcDataAdapter encounters an error generated by the server (Client-side errors are raised as standard common language runtime exceptions.). 它一律包含至少一個 OdbcError實例。It always contains at least one instance of OdbcError.

如果發生嚴重錯誤,伺服器可能會關閉 OdbcConnectionIf the severity of the error is too great, the server may close the OdbcConnection. 但是,使用者可以再次開啟連線,然後繼續進行。However, the user can reopen the connection and continue.

如需處理 .NET Framework Data Provider 例外狀況的一般資訊,請參閱 SqlExceptionFor general information about handling exceptions for a .NET Framework data provider, see SqlException.

屬性

Data

取得提供例外狀況之其他使用者定義相關資訊的索引鍵/值組集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(繼承來源 Exception)
ErrorCode

取得錯誤的 HRESULTGets the HRESULT of the error.

(繼承來源 ExternalException)
Errors

取得一個或多個 OdbcError 物件的集合,提供由 .NET Framework Data Provider for ODBC 所產生之例外狀況的相關詳細資訊。Gets a collection of one or more OdbcError objects that give detailed information about exceptions generated by the .NET Framework Data Provider for ODBC.

HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。Gets or sets a link to the help file associated with this exception.

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。Gets the Exception instance that caused the current exception.

(繼承來源 Exception)
Message

取得描述目前例外狀況的訊息。Gets a message that describes the current exception.

Source

取得產生錯誤的 ODBC 驅動程式名稱。Gets the name of the ODBC driver that generated the error.

StackTrace

取得呼叫堆疊上即時運算框架的字串表示。Gets a string representation of the immediate frames on the call stack.

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。Gets the method that throws the current exception.

(繼承來源 Exception)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 ExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(繼承來源 Exception)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

這個成員會覆寫 GetObjectData(SerializationInfo, StreamingContext)This member overrides GetObjectData(SerializationInfo, StreamingContext).

GetType()

取得目前執行個體的執行階段類型。Gets the runtime type of the current instance.

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

建立並傳回目前例外狀況的字串表示。Creates and returns a string representation of the current exception.

(繼承來源 Exception)

事件

SerializeObjectState

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(繼承來源 Exception)

適用於

另請參閱