Delen via


SqlConnection Constructors

Definition

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()

Initializes a new instance of the SqlConnection class.

public:
 SqlConnection();
public SqlConnection ();
Public Sub New ()

Examples

The following example creates and opens a SqlConnection.

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
        OpenSqlConnection();
        Console.ReadLine();
    }

    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.

Applies to

SqlConnection(String)

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

public:
 SqlConnection(System::String ^ connectionString);
public SqlConnection (string connectionString);
new Microsoft.Data.SqlClient.SqlConnection : string -> Microsoft.Data.SqlClient.SqlConnection
Public Sub New (connectionString As String)

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.

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
        OpenSqlConnection();
        Console.ReadLine();
    }

    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.

Applies to

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.

public:
 SqlConnection(System::String ^ connectionString, Microsoft::Data::SqlClient::SqlCredential ^ credential);
public SqlConnection (string connectionString, Microsoft.Data.SqlClient.SqlCredential credential);
new Microsoft.Data.SqlClient.SqlConnection : string * Microsoft.Data.SqlClient.SqlCredential -> Microsoft.Data.SqlClient.SqlConnection
Public Sub New (connectionString As String, credential As SqlCredential)

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 arguments failed validation, including ConnectionString validation.

Applies to