SqlConnection SqlConnection SqlConnection SqlConnection Constructors

定義

多載

SqlConnection() SqlConnection() SqlConnection()

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

SqlConnection(String) SqlConnection(String) SqlConnection(String) SqlConnection(String)

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

SqlConnection(String, SqlCredential) SqlConnection(String, SqlCredential) SqlConnection(String, SqlCredential) 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() 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會建立讀取/寫入屬性都設定為下列的初始值,除非它們特別設定使用其相關聯的關鍵字,在ConnectionString屬性。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 valueInitial value
ConnectionString 空字串 ("")empty string ("")
ConnectionTimeout 1515
Database 空字串 ("")empty string ("")
DataSource 空字串 ("")empty string ("")

您可以變更這些屬性的值只能透過ConnectionString屬性。You 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(String) SqlConnection(String) 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 String String 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會建立讀取/寫入屬性都設定為下列的初始值,除非它們特別設定使用其相關聯的關鍵字,在ConnectionString屬性。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 valueInitial value
ConnectionString connectionString
ConnectionTimeout 1515
Database 空字串 ("")empty string ("")
DataSource 空字串 ("")empty string ("")

您可以變更這些屬性的值只能透過ConnectionString屬性。You 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) SqlConnection(String, SqlCredential) SqlConnection(String, SqlCredential) 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 ^ cred);
public SqlConnection (string connectionString, System.Data.SqlClient.SqlCredential cred);
new System.Data.SqlClient.SqlConnection : string * System.Data.SqlClient.SqlCredential -> System.Data.SqlClient.SqlConnection
Public Sub New (connectionString As String, cred As SqlCredential)

參數

connectionString
String String String 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.

credential
SqlCredential SqlCredential SqlCredential 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).

另請參閱

適用於