OleDbException 類別

定義

當底層提供者對 OLE DB 資料來源回傳警告或錯誤時,會拋出的例外。 此類別無法獲得繼承。

public ref class OleDbException sealed : System::Runtime::InteropServices::ExternalException
public ref class OleDbException sealed : System::Data::Common::DbException
[System.Serializable]
public sealed class OleDbException : System.Runtime.InteropServices.ExternalException
[System.Serializable]
public sealed class OleDbException : System.Data.Common.DbException
[<System.Serializable>]
type OleDbException = class
    inherit ExternalException
[<System.Serializable>]
type OleDbException = class
    inherit DbException
Public NotInheritable Class OleDbException
Inherits ExternalException
Public NotInheritable Class OleDbException
Inherits DbException
繼承
繼承
屬性

範例

以下範例因缺少資料來源而產生 , OleDbException 並顯示例外。

public void ShowOleDbException()
{
   string mySelectQuery = "SELECT column1 FROM table1";
   OleDbConnection myConnection =
      new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=");
   OleDbCommand myCommand = new OleDbCommand(mySelectQuery,myConnection);

   try
   {
      myCommand.Connection.Open();
   }
   catch (OleDbException 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 + "\n" +
                          "Source: " + e.Errors[i].Source + "\n" +
                          "SQLState: " + 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 ShowOleDbException()
    Dim mySelectQuery As String = "SELECT column1 FROM table1"
    Dim myConnection As New OleDbConnection _
       ("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=")
    Dim myCommand As New OleDbCommand(mySelectQuery, myConnection)

    Try
        myCommand.Connection.Open()
    Catch e As OleDbException
        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 & ControlChars.Cr _
                           & "Source: " & e.Errors(i).Source & ControlChars.Cr _
                           & "SQLState: " & 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

備註

當 .NET Framework Data Provider for OLE DB 遇到伺服器產生錯誤時,會建立此類別。 (用戶端錯誤會作為標準的通用語言執行時例外拋出。) OleDbException 總是至少包含一個 的 OleDbError實例。

如果錯誤的嚴重性過大,伺服器可能會關閉 OleDbConnection。 不過,使用者可以重新開啟連線並繼續使用。

關於處理 .NET Framework 資料提供者例外的一般資訊,請參見 SqlException

屬性

名稱 Description
Data

取得索引鍵/值組的集合,提供例外狀況的其他使用者定義資訊。

(繼承來源 Exception)
ErrorCode

會取得錯誤的 HRESULT。

Errors

取得一個或多個 OleDbError 物件集合,提供 OLE DB .NET Framework Data Provider產生的異常細節。

HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,這是指派給特定例外狀況的編碼數值。

(繼承來源 Exception)
InnerException

會取得 Exception 造成目前例外的實例。

(繼承來源 Exception)
Message

會看到描述錯誤的文字。

Message

取得描述目前例外狀況的訊息。

(繼承來源 Exception)
Source

會取得產生錯誤的 OLE DB 提供者名稱。

Source

取得或設定造成錯誤之應用程式或物件的名稱。

(繼承來源 Exception)
StackTrace

取得呼叫堆疊上即時框架的字串表示。

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。

(繼承來源 Exception)

方法

名稱 Description
Equals(Object)

判斷指定的 物件是否等於目前的物件。

(繼承來源 Object)
GetBaseException()

當在派生類別中被覆寫時,回傳 Exception 是一個或多個後續例外的根因。

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

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

此成員覆蓋 GetObjectData(SerializationInfo, StreamingContext)

GetType()

取得目前實例的運行時間類型。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

回傳包含錯誤 HRESULT 的字串。

(繼承來源 ExternalException)

事件

名稱 Description
SerializeObjectState

發生於例外狀況串行化以建立例外狀況狀態物件,其中包含例外狀況的串行化數據。

(繼承來源 Exception)

適用於

另請參閱