OleDbException 类

定义

在基础提供程序返回 OLE DB 数据源的警告或错误时引发的异常。The exception that is thrown when the underlying provider returns a warning or error for an OLE DB data source. 无法继承此类。This class cannot be inherited.

public ref class OleDbException sealed : System::Data::Common::DbException
[System.Serializable]
public sealed class OleDbException : System.Data.Common.DbException
type OleDbException = class
    inherit DbException
Public NotInheritable Class OleDbException
Inherits DbException
继承
属性

示例

下面的示例由于缺少数据源而生成 OleDbException,并显示异常。The following example generates an OleDbException because of a missing data source, and then displays the exception.

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

注解

每当用于 OLE DB 的 .NET Framework 数据提供程序遇到服务器生成的错误时,就会创建此类。This class is created whenever the .NET Framework Data Provider for OLE DB encounters an error generated from the server. (客户端错误作为标准公共语言运行时异常引发。)OleDbException 始终包含至少一个 OleDbError实例。(Client side errors are thrown as standard common language runtime exceptions.) OleDbException always contains at least one instance of OleDbError.

如果错误的严重程度太高,服务器可能会关闭 OleDbConnectionIf the severity of the error is too great, the server may close the OleDbConnection. 但是,用户可以重新打开连接并继续操作。However, the user can reopen the connection and continue.

有关处理 .NET Framework 数据提供程序的异常的常规信息,请参阅 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

获取错误的 HRESULT。Gets the HRESULT of the error.

Errors

获取一个或多个 OleDbError 对象集合,这些对象提供有关用于 OLE DB 的 .NET Framework 数据提供程序生成的异常的详细信息。Gets a collection of one or more OleDbError objects that give detailed information about exceptions generated by the .NET Framework Data Provider for OLE DB.

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 the text describing the error.

Source

获取生成错误的 OLE DB 提供程序的名称。Gets the name of the OLE DB provider 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()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。When 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()

返回一个字符串,该字符串包含错误的 HRESULT。Returns a string that contains the HRESULT of the error.

(继承自 ExternalException)

事件

SerializeObjectState

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(继承自 Exception)

适用于

另请参阅