Share via


連接到 SQL Server 的執行個體

在 SQL Server Management Objects (SMO) 應用程式中的第一個程式設計步驟為建立 Server 物件的執行個體,並建立其與 MicrosoftSQL Server 執行個體的連接。

您可以用三種方式建立 Server 物件的執行個體,並建立 SQL Server 執行個體的連接。第一種方式是,使用 ServerConnection 物件變數來提供連接資訊。第二種方式是,明確地設定 Server 物件屬性來提供連接資訊。第三種方式是,在 Server 物件建構函式中傳遞 SQL Server 執行個體的名稱。

使用 ServerConnection 物件

使用 ServerConnection 物件變數的優點是,連接資訊可以重複使用。宣告 Server 物件變數。接著,宣告 ServerConnection 物件,並利用連接資訊 (例如,SQL Server 執行個體的名稱與驗證模式) 設定屬性。然後,傳遞 ServerConnection 物件變數,做為 Server 物件建構函式的參數。不建議同時在不同的伺服器物件之間共用連接。使用 Copy 方法來取得現有連接設定的複本。

明確地設定伺服器物件屬性

或者,您可以宣告 Server 物件變數,並呼叫預設的建構函式。同時,Server 物件會嘗試利用所有預設的連接設定,連接到 SQL Server 的預設執行個體。

在 Server 物件建構函式中提供 SQL Server 執行個體名稱

宣告 Server 物件變數,然後傳遞 SQL Server 執行個體名稱,做為建構函式中的字串參數。Server 物件會利用包含預設連接設定的 SQL Server 執行個體建立連接。

連接共用

通常不需要呼叫 ServerConnection 物件的 Connect 方法。SMO 將會在需要時,自動建立連接,並在執行作業完畢之後,將連接釋放到連接集區。呼叫 Connect 方法時,並不會將連接釋放到集區。若要將連接釋放到集區,必須明確地呼叫 Disconnect 方法。此外,您可以設定 ServerConnection 物件的 NonPooledConnection 屬性來要求非集區的連接。

多執行緒應用程式

對於多執行緒應用程式,應該在每個執行緒中使用個別的 ServerConnection 物件。

連接到 SQL Server for RMO 的執行個體

複寫管理物件 (RMO) 會使用與 SMO 稍微不同的方法來連接到複寫伺服器。

RMO 程式設計物件需要使用 Microsoft.SqlServer.Management.Common 命名空間實作的 ServerConnection 物件,來建立 SQL Server 執行個體的連接。這個伺服器連接會獨立於 RMO 程式設計物件之外建立。接著會在執行個體建立期間,或是將它指派到物件的 ConnectionContext 屬性,藉以將它傳遞到 RMO 物件。以此方式,就可以個別建立和管理 RMO 程式設計物件與連接物件執行個體,而且多個 RMO 程式設計物件可以重複使用單一連接物件。下列規則適用於應用程式伺服器的連接:

  • 連接的所有屬性是針對指定的 ServerConnection 物件所定義。

  • 每個 SQL Server 執行個體的連接都必須有它自己的 ServerConnection 物件。

  • ServerConnection 物件中,會提供所有建立連接及成功登入伺服器的驗證資訊。

  • 根據預設,連接都是使用「Microsoft Windows 驗證」所建立。若要使用 SQL Server 驗證,必須將 LoginSecure 設定為 False 與 Login,而且 Password 必須設定為有效的 SQL Server 登入與密碼。安全性認證必須以安全方式儲存和處理,而且每當有需要時必須在執行階段提供。

  • 您必須明確地呼叫 Connect 方法,才能將連接傳遞到任何 RMO 程式設計物件。

範例

如果要使用所提供的任何程式碼範例,您必須選擇建立應用程式用的程式設計環境、程式設計範本,及程式設計語言。如需詳細資訊,請參閱《SQL Server 線上叢書》中的<如何:在 Visual Studio .NET 中建立 Visual Basic SMO 專案>(英文) 或<如何:在 Visual Studio .NET 中建立 Visual C# SMO 專案>(英文)。

在 Visual Basic 中使用 Windows 驗證連接到 SQL Server 的本機執行個體

連接到 SQL Server 的本機執行個體不需要太多程式碼。而是依賴驗證方法和伺服器的預設值。需要擷取資料的第一個作業將會導致連接建立。

此範例為 Visual Basic .NET 程式碼,可使用 Windows 驗證連接到 SQL Server 的本機執行個體。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'The connection is established when a property is requested.
Console.WriteLine(srv.Information.Version)
'The connection is automatically disconnected when the Server variable goes out of scope.

在 Visual C# 中使用 Windows 驗證連接到 SQL Server 的本機執行個體

連接到 SQL Server 的本機執行個體不需要太多程式碼。而是依賴驗證方法和伺服器的預設值。需要擷取資料的第一個作業將會導致連接建立。

此範例為 Visual C# .NET 程式碼,會用 Windows 驗證連接到 SQL Server 的本機執行個體。

{ 
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//The connection is established when a property is requested. 
Console.WriteLine(srv.Information.Version); 
} 
//The connection is automatically disconnected when the Server variable goes out of scope.

在 Visual Basic 中使用 Windows 驗證連接到 SQL Server 的遠端執行個體

當您使用 Windows 驗證連接到 SQL Server 的執行個體時,您不需要指定驗證類型。Windows 驗證是預設值。

此連接為 Visual Basic .NET 程式碼,可使用 Windows 驗證連接到 SQL Server 的遠端執行個體。字串變數 strServer 包含遠端執行個體的名稱。

'Connect to a remote instance of SQL Server.
Dim srv As Server
'The strServer string variable contains the name of a remote instance of SQL Server.
srv = New Server(strServer)
'The actual connection is made when a property is retrieved. 
Console.WriteLine(srv.Information.Version)
'The connection is automatically disconnected when the Server variable goes out of scope.

在 Visual C# 中使用 Windows 驗證連接到 SQL Server 的遠端執行個體

當您使用 Windows 驗證連接到 SQL Server 的執行個體時,您不需要指定驗證類型。Windows 驗證是預設值。

此範例為 Visual C# .NET 程式碼,會使用 Windows 驗證連接到 SQL Server 的遠端執行個體。字串變數 strServer 包含遠端執行個體的名稱。

{ 
//Connect to a remote instance of SQL Server. 
Server srv; 
//The strServer string variable contains the name of a remote instance of SQL Server. 
srv = new Server(strServer); 
//The actual connection is made when a property is retrieved. 
Console.WriteLine(srv.Information.Version); 
} 
//The connection is automatically disconnected when the Server variable goes out of scope.

在 Visual Basic 中使用 SQL Server 驗證連接到 SQL Server 的執行個體

當您使用 SQL Server 驗證連接到 SQL Server 的執行個體時,您必須指定驗證類型。此範例示範宣告 ServerConnection 物件變數的替代方法,讓連接資訊得以重複使用。

此範例是示範如何連接到遠端的 Visual Basic .NET 程式碼,而 vPassword 則包含登入和密碼。

'Declare a ServerConnection object variable to specify SQL authentication, login and password.
Dim conn As New ServerConnection
conn.LoginSecure = False
conn.Login = vlogin
conn.Password = vpassword
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server(conn)
'The actual connection is made when a property is retrieved.
Console.WriteLine(srv.Information.Version)
'The connection is automatically disconnected when the Server variable goes out of scope.

在 Visual C# 中使用 SQL Server 驗證連接到 SQL Server 的執行個體

當您使用 SQL Server 驗證連接到 SQL Server 的執行個體時,您必須指定驗證類型。此範例示範宣告 ServerConnection 物件變數的替代方法,讓連接資訊得以重複使用。

此範例是示範如何連接到遠端的 Visual C# .NET 程式碼,而 vPassword 則包含登入和密碼。

{ 
//Connect to a remote instance of SQL Server. 
Server srv; 
//The strServer string variable contains the name of a remote instance of SQL Server. 
srv = new Server(strServer); 
//The actual connection is made when a property is retrieved. 
Console.WriteLine(srv.Information.Version); 
} 
//The connection is automatically disconnected when the Server variable goes out of scope.

請參閱

參考