OleDbException 类

在基础提供程序返回 OLE DB 数据源的警告或错误时引发的异常。无法继承此类。

**命名空间:**System.Data.OleDb
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
<SerializableAttribute> _
Public NotInheritable Class OleDbException
    Inherits DbException
用法
Dim instance As OleDbException
[SerializableAttribute] 
public sealed class OleDbException : DbException
[SerializableAttribute] 
public ref class OleDbException sealed : public DbException
/** @attribute SerializableAttribute() */ 
public final class OleDbException extends DbException
SerializableAttribute 
public final class OleDbException extends DbException

备注

每当用于 OLE DB 的 .NET Framework 数据提供程序遇到服务器生成的错误时,就会创建此类。(客户端错误作为标准的公共语言运行库异常引发。)OleDbException 始终包含至少一个 OleDbError 实例。

如果错误太严重,服务器可能会关闭 OleDbConnection。但是,用户可以重新打开连接并继续操作。

示例

下面的示例由于缺少数据源而生成一个 OleDbException,然后显示该异常。

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 System.Diagnostics.EventLog = 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
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.");
   }
}

继承层次结构

System.Object
   System.Exception
     System.SystemException
       System.Runtime.InteropServices.ExternalException
         System.Data.Common.DbException
          System.Data.OleDb.OleDbException

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

OleDbException 成员
System.Data.OleDb 命名空间
OleDbErrorCollection 类

其他资源

异常概述