OdbcException OdbcException OdbcException OdbcException Class


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
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);
   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";
     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)
    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"
       Console.WriteLine("An exception occurred. Please contact your system administrator.")
    End Try
End Sub


只要OdbcDataAdapter遇到服务器生成的错误(客户端错误作为标准公共语言运行时异常引发),就会创建此类。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.). 它始终至少包含一个实例OdbcErrorIt 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 数据提供程序的异常的常规信息,请SqlException参阅。For general information about handling exceptions for a .NET Framework data provider, see SqlException.


Data Data Data Data

获取提供有关异常的其他用户定义信息的键/值对集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(Inherited from Exception)
Errors Errors Errors Errors

获取一个或多个 OdbcError 对象的集合,这些对象提供有关用于 ODBC 的 .NET Framework 数据提供程序生成的异常的详细信息。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 HelpLink HelpLink HelpLink

获取或设置指向与此异常关联的帮助文件链接。Gets or sets a link to the help file associated with this exception.

(Inherited from Exception)
HResult HResult HResult HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(Inherited from Exception)
InnerException InnerException InnerException InnerException

获取导致当前异常的 Exception 实例。Gets the Exception instance that caused the current exception.

(Inherited from Exception)
Message Message Message Message

获取描述当前异常的消息。Gets a message that describes the current exception.

Source Source Source Source

获取生成错误的 ODBC 驱动程序的名称。Gets the name of the ODBC driver that generated the error.

StackTrace StackTrace StackTrace StackTrace

获取调用堆栈上的即时框架字符串表示形式。Gets a string representation of the immediate frames on the call stack.

(Inherited from Exception)
TargetSite TargetSite TargetSite TargetSite

获取引发当前异常的方法。Gets the method that throws the current exception.

(Inherited from Exception)


Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetBaseException() GetBaseException() GetBaseException() GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根源。When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(Inherited from Exception)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

此成员重写 GetObjectData(SerializationInfo, StreamingContext)This member overrides GetObjectData(SerializationInfo, StreamingContext).

GetType() GetType() GetType() GetType()

获取当前实例的运行时类型。Gets the runtime type of the current instance.

(Inherited from Exception)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

创建并返回当前异常的字符串表示形式。Creates and returns a string representation of the current exception.

(Inherited from Exception)


SerializeObjectState SerializeObjectState SerializeObjectState SerializeObjectState

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

(Inherited from Exception)