SqlConnection Constructors

Definition

Initializes a new instance of the SqlConnection class.

Overloads

SqlConnection()

Initializes a new instance of the SqlConnection class.

SqlConnection(String)

Initializes a new instance of the SqlConnection class when given a string that contains the connection string.

SqlConnection(String, SqlCredential)

Initializes a new instance of the SqlConnection class given a connection string, that does not use Integrated Security = true and a SqlCredential object that contains the user ID and password.

SqlConnection()

Source:
System.Data.SqlClient.notsupported.cs

Initializes a new instance of the SqlConnection class.

C#
public SqlConnection();

Examples

The following example creates and opens a SqlConnection.

C#
private static void OpenSqlConnection()
{
    string connectionString = GetConnectionString();
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
        Console.WriteLine("State: {0}", connection.State);
    }
}

static private string GetConnectionString()
{
    // To avoid storing the connection string in your code,
    // you can retrieve it from a configuration file, using the
    // System.Configuration.ConfigurationManager.ConnectionStrings property
    return "Data Source=(local);Initial Catalog=AdventureWorks;"
        + "Integrated Security=SSPI;";
}

Remarks

When a new instance of SqlConnection 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. The SqlConnectionStringBuilder class provides functionality for creating and managing the contents of connection strings.

See also

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

SqlConnection(String)

Source:
System.Data.SqlClient.notsupported.cs

Initializes a new instance of the SqlConnection class when given a string that contains the connection string.

C#
public SqlConnection(string connectionString);

Parameters

connectionString
String

The connection used to open the SQL Server database.

Exceptions

The supplied connection string argument failed ConnectionString validation.

Examples

The following example creates and opens a SqlConnection.

C#
private static void OpenSqlConnection()
{
    string connectionString = GetConnectionString();

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();

        Console.WriteLine("State: {0}", connection.State);
        Console.WriteLine("ConnectionString: {0}",
            connection.ConnectionString);
    }
}

static private string GetConnectionString()
{
    // To avoid storing the connection string in your code,
    // you can retrieve it from a configuration file, using the
    // System.Configuration.ConfigurationSettings.AppSettings property
    return "Data Source=(local);Initial Catalog=AdventureWorks;"
        + "Integrated Security=SSPI;";
}

Remarks

When a new instance of SqlConnection 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. The SqlConnection class provides functionality for creating and managing the contents of connection strings.

See also

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

SqlConnection(String, SqlCredential)

Source:
System.Data.SqlClient.notsupported.cs

Initializes a new instance of the SqlConnection class given a connection string, that does not use Integrated Security = true and a SqlCredential object that contains the user ID and password.

C#
public SqlConnection(string connectionString, System.Data.SqlClient.SqlCredential credential);

Parameters

connectionString
String

A connection string that does not use any of the following connection string keywords: Integrated Security = true, UserId, or Password; or that does not use ContextConnection = true.

credential
SqlCredential

A SqlCredential object. If credential is null, SqlConnection(String, SqlCredential) is functionally equivalent to SqlConnection(String).

Exceptions

The supplied connection string argument failed ConnectionString validation.

See also

Applies to

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