OleDbConnection Constructors

Definition

Initializes a new instance of the OleDbConnection class.

Overloads

OleDbConnection()

Initializes a new instance of the OleDbConnection class.

OleDbConnection(String)

Initializes a new instance of the OleDbConnection class with the specified connection string.

OleDbConnection()

Source:
DbConnectionHelper.cs
Source:
DbConnectionHelper.cs
Source:
DbConnectionHelper.cs

Initializes a new instance of the OleDbConnection class.

C#
public OleDbConnection();

Examples

The following example creates and opens an OleDbConnection.

C#
static void OpenConnection(string connectionString)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        try
        {
            connection.Open();
            Console.WriteLine("DataSource: {0} \nDatabase: {1}",
                connection.DataSource, connection.Database);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }
}

Remarks

When a new instance of OleDbConnection is created, the read/write properties are set to the following initial values unless they are specifically set using their associated keywords in the ConnectionString property.

Properties Initial value
ConnectionString empty string ("")
ConnectionTimeout 15
Database empty string ("")
DataSource empty string ("")

You can change the value for these properties only by using the ConnectionString property.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

OleDbConnection(String)

Source:
OleDbConnection.cs
Source:
OleDbConnection.cs
Source:
OleDbConnection.cs

Initializes a new instance of the OleDbConnection class with the specified connection string.

C#
public OleDbConnection(string? connectionString);
C#
public OleDbConnection(string connectionString);

Parameters

connectionString
String

The connection used to open the database.

Examples

The following example creates and opens an OleDbConnection.

C#
static void OpenConnection(string connectionString)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        try
        {
            connection.Open();
            Console.WriteLine("DataSource: {0} \nDatabase: {1}",
                connection.DataSource, connection.Database);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }
}

Remarks

When a new instance of OleDbConnection is created, the read/write properties are set to the following initial values unless they are specifically set using their associated keywords in the ConnectionString property.

Properties Initial value
ConnectionString connectionString
ConnectionTimeout 15
Database empty string ("")
DataSource empty string ("")

You can change the value for these properties only by using the ConnectionString property.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)