Export (0) Print
Expand All

OleDbConnection Class

Represents an open connection to a data source.

Namespace: System.Data.OleDb
Assembly: System.Data (in system.data.dll)

public sealed class OleDbConnection : DbConnection, ICloneable, IDbConnection, IDisposable
public final class OleDbConnection extends DbConnection implements ICloneable, IDbConnection, 
	IDisposable
public final class OleDbConnection extends DbConnection implements ICloneable, IDbConnection, 
	IDisposable

An OleDbConnection object represents a unique connection to a data source. With 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, or by using the OleDbConnection object within a Using statement.

NoteNote

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

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 might 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 sufficient 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 sufficient 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 computer, user, and enterprise levels. For more information, see Code Access Security and ADO.NET.

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

NoteNote

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.

The following example creates an OleDbCommand and an 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, ExecuteNonQuery is passed a connection string and a query string that is an SQL INSERT statement.

public void InsertRow(string connectionString, string insertSQL)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        // The insertSQL string contains a SQL statement that
        // inserts a new row in the source table.
        OleDbCommand command = new OleDbCommand(insertSQL);

        // Set the Connection to the new OleDbConnection.
        command.Connection = connection;

        // Open the connection and execute the insert command.
        try
        {
            connection.Open();
            command.ExecuteNonQuery();
        
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        
        // The connection is automatically closed when the
        // code exits the using block.
    

using System;
using System.Data;
using System.Data.OleDb;

class Class1
{
    static void Main()
    {
    

    public void InsertRow(string connectionString, string insertSQL)
    {
        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            // The insertSQL string contains a SQL statement that
            // inserts a new row in the source table.
            OleDbCommand command = new OleDbCommand(insertSQL);

            // Set the Connection to the new OleDbConnection.
            command.Connection = connection;

            // Open the connection and execute the insert command.
            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            
            // The connection is automatically closed when the
            // code exits the using block.
        

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

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

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft