SqlConnection 建構函式

定義

初始化 SqlConnection 類別的新執行個體。Initializes a new instance of the SqlConnection class.

多載

SqlConnection()

初始化 SqlConnection 類別的新執行個體。Initializes a new instance of the SqlConnection class.

SqlConnection(String)

指定包含連接字串的字串時,初始化 SqlConnection 類別的新執行個體。Initializes a new instance of the SqlConnection class when given a string that contains the connection string.

SqlConnection(String, SqlCredential)

指定不使用 Integrated Security = true 和包含使用者 ID 和密碼之 SqlCredential 物件的連接字串,初始化 SqlConnection 類別的新執行個體。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()

初始化 SqlConnection 類別的新執行個體。Initializes a new instance of the SqlConnection class.

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

範例

下列範例會建立並開啟 SqlConnectionThe 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

備註

當建立新的實例時 SqlConnection ,讀取/寫入屬性會設定為下列初始值,除非在屬性中使用其相關聯的關鍵字特別設定這些值 ConnectionStringWhen 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 valueInitial value
ConnectionString 空字串 ( "" ) empty string ("")
ConnectionTimeout 1515
Database 空字串 ( "" ) empty string ("")
DataSource 空字串 ( "" ) empty string ("")

您只能使用屬性來變更這些屬性的值 ConnectionStringYou can change the value for these properties only by using the ConnectionString property. SqlConnectionStringBuilder類別會提供用來建立和管理連接字串內容的功能。The SqlConnectionStringBuilder class provides functionality for creating and managing the contents of connection strings.

另請參閱

適用於

SqlConnection(String)

指定包含連接字串的字串時,初始化 SqlConnection 類別的新執行個體。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)

參數

connectionString
String

開啟 SQL Server 資料庫所使用的連線。The connection used to open the SQL Server database.

範例

下列範例會建立並開啟 SqlConnectionThe 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

備註

當建立新的實例時 SqlConnection ,讀取/寫入屬性會設定為下列初始值,除非在屬性中使用其相關聯的關鍵字特別設定這些值 ConnectionStringWhen 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 valueInitial value
ConnectionString connectionString
ConnectionTimeout 1515
Database 空字串 ( "" ) empty string ("")
DataSource 空字串 ( "" ) empty string ("")

您只能使用屬性來變更這些屬性的值 ConnectionStringYou can change the value for these properties only by using the ConnectionString property. SqlConnection類別會提供用來建立和管理連接字串內容的功能。The SqlConnection class provides functionality for creating and managing the contents of connection strings.

另請參閱

適用於

SqlConnection(String, SqlCredential)

指定不使用 Integrated Security = true 和包含使用者 ID 和密碼之 SqlCredential 物件的連接字串,初始化 SqlConnection 類別的新執行個體。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(System::String ^ connectionString, System::Data::SqlClient::SqlCredential ^ cred);
public SqlConnection (string connectionString, System.Data.SqlClient.SqlCredential credential);
public SqlConnection (string connectionString, System.Data.SqlClient.SqlCredential cred);
new System.Data.SqlClient.SqlConnection : string * System.Data.SqlClient.SqlCredential -> System.Data.SqlClient.SqlConnection
new System.Data.SqlClient.SqlConnection : string * System.Data.SqlClient.SqlCredential -> System.Data.SqlClient.SqlConnection
Public Sub New (connectionString As String, credential As SqlCredential)
Public Sub New (connectionString As String, cred As SqlCredential)

參數

connectionString
String

不使用下列任何連接字串關鍵字的連接字串:Integrated Security = trueUserIdPassword;或者不使用 ContextConnection = true 的連接字串。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.

credentialcred
SqlCredential

SqlCredential 物件。A SqlCredential object. 如果 credential 為 Null,SqlConnection(String, SqlCredential) 在功能上相當於 SqlConnection(String)If credential is null, SqlConnection(String, SqlCredential) is functionally equivalent to SqlConnection(String).

另請參閱

適用於