Upravit

Sdílet prostřednictvím


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.

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

Examples

The following example creates and opens a SqlConnection.

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;";
}
Private Sub OpenSqlConnection()
    Dim connectionString As String = GetConnectionString()
    Using connection As New SqlConnection(connectionString)
        connection.Open()
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion)
        Console.WriteLine("State: {0}", connection.State)
    End Using
End Sub

Private Function GetConnectionString() As String
    ' 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);Database=AdventureWorks;" _
      & "Integrated Security=SSPI;"
End Function

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

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.

public:
 SqlConnection(System::String ^ connectionString);
public SqlConnection (string connectionString);
new System.Data.SqlClient.SqlConnection : string -> System.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.

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;";
}
Private Sub OpenSqlConnection()
    Dim connectionString As String = GetConnectionString()

    Using connection As New SqlConnection(connectionString)

        connection.Open()

        Console.WriteLine("State: {0}", connection.State)
        Console.WriteLine("ConnectionString: {0}", _
            connection.ConnectionString)
    End Using
End Sub

Private Function GetConnectionString() As String
    ' 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);Database=AdventureWorks;" _
      & "Integrated Security=SSPI;"
End Function

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

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.

public:
 SqlConnection(System::String ^ connectionString, System::Data::SqlClient::SqlCredential ^ credential);
public SqlConnection (string connectionString, System.Data.SqlClient.SqlCredential credential);
new System.Data.SqlClient.SqlConnection : string * System.Data.SqlClient.SqlCredential -> System.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 connection string argument failed ConnectionString validation.

See also

Applies to