OleDbException 類別

定義

基礎提供者傳回 OLE DB 資料來源的警告或錯誤時所擲回的例外狀況。 此類別無法獲得繼承。

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

範例

下列範例會產生 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 遇到從伺服器產生的錯誤時,就會建立這個類別。 (用戶端錯誤會擲回為標準 Common Language Runtime exceptions.) OleDbException 一律包含至少一個 實例 OleDbError

如果發生嚴重錯誤,伺服器可能會關閉 OleDbConnection。 但是,使用者可以再次開啟連線,然後繼續進行。

如需處理.NET Framework資料提供者例外狀況的一般資訊,請參閱 SqlException

屬性

BatchCommand

如果在執行 DbBatch 時擲回這個 DbException 值,則參考觸發例外狀況的特定 DbBatchCommand

(繼承來源 DbException)
Data

取得鍵值組的集合,這些鍵值組會提供關於例外狀況的其他使用者定義資訊。

(繼承來源 Exception)
DbBatchCommand

在衍生類別中覆寫時,如果在執行 DbBatch 時擲回, DbException 則會參考觸發例外狀況的特定 DbBatchCommand

(繼承來源 DbException)
ErrorCode

取得錯誤的 HRESULT。

Errors

取得一個或多個 OleDbError 物件的集合,提供由 .NET Framework Data Provider for OLE DB 所產生之例外狀況的相關詳細資訊。

HelpLink

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

(繼承來源 Exception)
HResult

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

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。

(繼承來源 Exception)
IsTransient

指出此 DbException 所代表的錯誤是否可能是暫時性錯誤,亦即,重試觸發作業是否可能會成功,而不需要任何其他變更。

(繼承來源 DbException)
Message

取得描述錯誤的文字。

Message

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

(繼承來源 Exception)
Source

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

Source

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

(繼承來源 Exception)
SqlState

針對支援的資料庫提供者,包含標準 SQL 5 字元傳回碼,指出資料庫作業成功或失敗。 前 2 個字元代表傳回碼的類別 (例如錯誤、成功),而最後 3 個字元代表子類別,允許以資料庫可移植的方式偵測錯誤案例。

針對不支援的資料庫提供者,或不適用的錯誤案例,包含 null

(繼承來源 DbException)
StackTrace

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

(繼承來源 Exception)
TargetSite

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

(繼承來源 Exception)

方法

Equals(Object)

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

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 Exception

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

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

這個成員會覆寫 GetObjectData(SerializationInfo, StreamingContext)

GetType()

取得目前執行個體的執行階段類型。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

建立並傳回目前例外狀況的字串表示。

(繼承來源 Exception)
ToString()

傳回字串,其中包含錯誤的 HRESULT。

(繼承來源 ExternalException)

事件

SerializeObjectState
已淘汰.

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。

(繼承來源 Exception)

適用於

另請參閱