Export (0) Print
Expand All
15 out of 44 rated this helpful - Rate this topic

OleDbConnection Class

Represents an open connection to a data source.

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

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Data.OleDb.OleDbConnection

[Visual Basic]
NotInheritable Public Class OleDbConnection
   Inherits Component
   Implements ICloneable, IDbConnection
[C#]
public sealed class OleDbConnection : Component, ICloneable,
   IDbConnection
[C++]
public __gc __sealed class OleDbConnection : public Component,
   ICloneable, IDbConnection
[JScript]
public class OleDbConnection 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.

Remarks

An OleDbConnection object represents a unique connection to a data source. 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 OLE DB provider, some methods, or properties of an OleDbConnection object may not be available.

When you create an instance of OleDbConnection, all properties are set to their initial values. For a list of these values, see the OleDbConnection constructor.

If the OleDbConnection goes out of scope, it is not closed. Therefore, you must explicitly close the connection by calling Close or Dispose.

Note   To deploy high-performance applications, you need to use connection pooling. When you use the .NET Framework Data Provider for OLE DB, you do not need to enable connection pooling because the provider manages this automatically. For more information about using connection pooling with the .NET Framework Data Provider for OLE DB, see Connection Pooling for the .NET Framework Data Provider for OLE DB.

If a fatal OleDbException (for example, a SQL Server severity level of 20 or greater) is generated by the method executing an OleDbCommand, the OleDbConnection, the connection may be closed. However, the user can reopen the connection and continue.

An application that creates an instance of the OleDbConnection object can require all direct and indirect callers to have adequate permission to the code by setting declarative or imperative security demands. OleDbConnection makes security demands using the OleDbPermission object. Users can verify that their code has adequate permissions by using the OleDbPermissionAttribute 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 server, see Working with Connection Events.

Note   The OleDbConnection object does not support setting or retrieving dynamic properties specific to an OLE DB provider. Only properties that can be passed in the connection string for the OLE DB provider are supported.

Example

[Visual Basic, C#, C++] The following example creates a OleDbCommand and a OleDbConnection. The OleDbConnection is opened and set as the Connection for the OleDbCommand. 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(myConnectionString As String)
    ' If the connection string is null, use a default.
    If myConnectionString = "" Then
        myConnectionString = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"
    End If
    Dim myConnection As New OleDbConnection(myConnectionString)
    Dim myInsertQuery As String = "INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')"
    Dim myCommand As New OleDbCommand(myInsertQuery)
    myCommand.Connection = myConnection
    myConnection.Open()
    myCommand.ExecuteNonQuery()
    myCommand.Connection.Close()
End Sub

[C#] 
public void InsertRow(string myConnectionString) 
{
   // If the connection string is null, use a default.
   if(myConnectionString == "") 
   {
      myConnectionString = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Northwind;" + 
      "Integrated Security=SSPI;";
   }
   OleDbConnection myConnection = new OleDbConnection(myConnectionString);
   string myInsertQuery = "INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')";
   OleDbCommand myCommand = new OleDbCommand(myInsertQuery);
   myCommand.Connection = myConnection;
   myConnection.Open();
   myCommand.ExecuteNonQuery();
   myCommand.Connection.Close();
}

[C++] 
public:
 void InsertRow(String* myConnectionString) 
 {
    // If the connection string is null, use a default.
    if(myConnectionString->Equals(S"")) 
    {
       myConnectionString = S"Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Northwind;"  
       S"Integrated Security=SSPI;";
    }
    OleDbConnection* myConnection = new OleDbConnection(myConnectionString);
    String* myInsertQuery = S"INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')";
    OleDbCommand* myCommand = new OleDbCommand(myInsertQuery);
    myCommand->Connection = myConnection;
    myConnection->Open();
    myCommand->ExecuteNonQuery();
    myCommand->Connection->Close();
 }

[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.

Requirements

Namespace: System.Data.OleDb

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

OleDbConnection Members | System.Data.OleDb Namespace | OleDbDataAdapter | OleDbCommand

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.