This documentation is archived and is not being maintained.

OdbcConnection Class

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

Represents an open connection to a data source.

For a list of all members of this type, see OdbcConnection Members.


[Visual Basic]
NotInheritable Public Class OdbcConnection
   Inherits Component
   Implements ICloneable, IDbConnection
public sealed class OdbcConnection : Component, ICloneable,
public __gc __sealed class OdbcConnection : public Component,
   ICloneable, IDbConnection
public class OdbcConnection extends Component implements
   ICloneable, IDbConnection

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


An OdbcConnection object represents a unique connection to a data source created by using a connection string or ODBC data source name (DSN). In the case of a client/server database system, it is equivalent to a network connection to the server. Depending on the functionality supported by the native ODBC driver, some methods or properties of an OdbcConnection object may not be available.

The OdbcConnection object uses native resources such as ODBC environment and connection handles. You should always explicitly close any open OdbcConnection objects by calling Close or Dispose before the OdbcConnection object goes out of scope. Not doing so leaves the freeing of these native resources to garbage collection, which may not free them immediately. This, in turn, may eventually cause the underlying driver to run out of resources or reach a maximum limit, resulting in sporadic failures. For example, you might encounter Maximum Connections-related errors while a number of connections are waiting to be deleted by the garbage collector. Explicitly closing the connections by calling Close or Dispose allows a more efficient use of native resources, enhancing scalability and improving overall application performance.

Note   To deploy high-performance applications, you often need to use connection pooling. However, when you use the .NET Framework Data Provider for ODBC, you do not need to enable connection pooling because the provider manages this automatically. For more information about enabling and disabling connection pooling, see the Microsoft Open Database Connectivity (ODBC) documentation.

If one of the Execute methods of the OdbcCommand class results in a fatal OdbcException (for example, a SQL Server severity level of 20 or greater), the OdbcConnection may close. However, the user can reopen the connection and continue.

An application that creates an instance of the OdbcConnection object can require all direct and indirect callers to have adequate permission to the code by setting declarative or imperative security demands. OdbcConnection creates security demands by using the OdbcPermission object. Users can verify that their code has adequate permissions by using the OdbcPermissionAttribute object. Users and administrators can also use the Code Access Security Policy Tool (Caspol.exe) to modify security policy at the machine, user, and enterprise levels. For more information, see Securing Applications.

For more information about handling warning and informational messages from the data source, see Working with Connection Events.


[Visual Basic, C#, C++] The following example creates an OdbcCommand and an OdbcConnection. The OdbcConnection is opened and set as the OdbcCommand.Connection property. The example then calls ExecuteNonQuery, and closes the connection. To accomplish this, the ExecuteNonQuery is passed a connection string and a query string that is an SQL INSERT statement.

[Visual Basic] 
Public Sub InsertRow(myConnection As String)
    ' If the connection string is null, use a default.
    If myConnection = "" Then
        myConnection = "DRIVER={SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;"
    End If
    Dim myConn As New OdbcConnection(myConnection)
    Dim myInsertQuery As String = "INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')"
    Dim myOdbcCommand As New OdbcCommand(myInsertQuery)
    myOdbcCommand.Connection = myConn
End Sub

public void InsertRow(string myConnection) 
   // If the connection string is null, use a default.
   if(myConnection == "") 
      myConnection = "DRIVER={SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;";
   OdbcConnection myConn = new OdbcConnection(myConnection);
   string myInsertQuery = "INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')";
   OdbcCommand myOdbcCommand = new OdbcCommand(myInsertQuery);
   myOdbcCommand.Connection = myConn;

    void InsertRow(String* myConnection)
        // If the connection string is null, use a default.
        if (myConnection->Equals(S""))
            myConnection = S"DRIVER= {SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;";
        OdbcConnection* myConn = new OdbcConnection(myConnection);
        String* myInsertQuery = S"INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')";
        OdbcCommand* myOdbcCommand = new OdbcCommand(myInsertQuery);
        myOdbcCommand->Connection = myConn;

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Data.Odbc

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Data (in System.Data.dll)

See Also

OdbcConnection Members | System.Data.Odbc Namespace | OdbcDataAdapter | OdbcCommand