共用方式為


連接到 SQL Server 執行個體

SQL Server 管理物件 (SMO) 應用程式中的第一個程式設計步驟是建立 對象的實例 Server ,並建立其與 Microsoft SQL Server 實例的連接。

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

使用 ServerConnection 物件

使用 ServerConnection 物件變數的優點是可以重複使用連接資訊。 Server宣告物件變數。 然後,使用連接資訊宣告 ServerConnection 對象並設定屬性,例如 SQL Server 實例的名稱和驗證模式。 然後,將 ServerConnection 物件變數當做參數傳遞至 Server 物件建構函式。 不建議同時共用不同伺服器對象之間的連線。 Copy使用方法來取得現有連線設定的複本。

明確設定 Server 物件屬性

或者,您可以宣告 Server 物件變數並呼叫預設建構函式。 如同, Server 物件會嘗試使用所有預設連接設定連接到 SQL Server 的預設實例。

在 Server 物件建構函式中提供 SQL Server 實例名稱

宣告物件變數, Server 並將 SQL Server 實例名稱傳遞為建構函式中的字串參數。 物件 Server 會使用預設連接設定,建立與 SQL Server 實例的連接。

連線池化

通常不需要呼叫 Connect 物件的方法 ServerConnection 。 SMO 會在需要時自動建立連線,並在連線集區完成執行作業之後釋放連線。 Connect呼叫 方法時,不會將連線釋放至集區。 需要明確呼叫 Disconnect 方法,才能釋放與集區的連線。 此外,您可以藉由設定 NonPooledConnection 對象的屬性 ServerConnection 來要求非集區連線。

多線程應用程式

對於多線程應用程式,應該在每個線程中使用個別 ServerConnection 物件。

連接到 RMO 的 SQL Server 實例

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

RMO 程式設計物件需要使用 命名空間所實作的物件,建立 ServerConnection SQL Server 實例的連接 Microsoft.SqlServer.Management.Common 。 此與伺服器的連線是由 RMO 程式設計物件所獨立建立。 然後,它會在實例建立期間或指派給 ConnectionContext 對象的屬性,傳遞給 RMO 物件。 如此一來,就可以個別建立及管理 RMO 程式設計對象和連接物件實例,而且單一連接物件可以與多個 RMO 程式設計物件重複使用。 下列規則適用於復寫伺服器的連線:

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

  • 每個與 SQL Server 實例的連接都必須有自己的 ServerConnection 物件。

  • 在物件中 ServerConnection 會提供連線並成功登入伺服器的所有驗證資訊。

  • 根據預設,會使用 Microsoft Windows 驗證來建立連線。 若要使用 SQL Server 驗證,LoginSecure必須設定為 False,而且LoginPassword必須設定為有效的 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 的本機實例不需要太多程式代碼。 相反地,它會依賴驗證方法和伺服器的預設設定。 需要擷取數據的第一個作業會導致建立連線。

此範例是使用 Windows 驗證連線到 SQL Server 本機實例的 Visual Basic .NET 程式代碼。

在 Visual C 中使用 Windows 驗證連線到 SQL Server 的本機實例#

連接到 SQL Server 的本機實例不需要太多程式代碼。 相反地,它會依賴驗證方法和伺服器的預設設定。 需要擷取數據的第一個作業會導致建立連線。

此範例是使用 Windows 驗證連線到 SQL Server 本機實例的 Visual C# .NET 程式代碼。

{   
//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 驗證是預設值。

此範例是使用 Windows 驗證連線到 SQL Server 遠端實例的 Visual Basic .NET 程式代碼。 字串變數 strServer 包含遠端實例的名稱。

在 Visual C 中使用 Windows 驗證連線到 SQL Server 的遠端實例#

當您使用 Windows 驗證連線到 SQL Server 實例時,不需要指定驗證類型。 Windows 驗證是預設值。

此範例是使用 Windows 驗證連線到 SQL Server 遠端實例的 Visual C# .NET 程式代碼。 字串變數 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 程式代碼,示範如何連線到 remote 和 vPassword 包含登入和密碼。

' compile with:   
' /r:Microsoft.SqlServer.Smo.dll  
' /r:Microsoft.SqlServer.ConnectionInfo.dll  
' /r:Microsoft.SqlServer.Management.Sdk.Sfc.dll   
  
Imports Microsoft.SqlServer.Management.Smo  
Imports Microsoft.SqlServer.Management.Common  
  
Public Class A  
   Public Shared Sub Main()  
      Dim sqlServerLogin As [String] = "user_id"  
      Dim password As [String] = "pwd"  
      Dim instanceName As [String] = "instance_name"  
      Dim remoteSvrName As [String] = "remote_server_name"  
  
      ' Connecting to an instance of SQL Server using SQL Server Authentication  
      Dim srv1 As New Server()   ' connects to default instance  
      srv1.ConnectionContext.LoginSecure = False   ' set to true for Windows Authentication  
      srv1.ConnectionContext.Login = sqlServerLogin  
      srv1.ConnectionContext.Password = password  
      Console.WriteLine(srv1.Information.Version)   ' connection is established  
  
      ' Connecting to a named instance of SQL Server with SQL Server Authentication using ServerConnection  
      Dim srvConn As New ServerConnection()  
      srvConn.ServerInstance = ".\" & instanceName   ' connects to named instance  
      srvConn.LoginSecure = False   ' set to true for Windows Authentication  
      srvConn.Login = sqlServerLogin  
      srvConn.Password = password  
      Dim srv2 As New Server(srvConn)  
      Console.WriteLine(srv2.Information.Version)   ' connection is established  
  
      ' For remote connection, remote server name / ServerInstance needs to be specified  
      Dim srvConn2 As New ServerConnection(remoteSvrName)  
      srvConn2.LoginSecure = False  
      srvConn2.Login = sqlServerLogin  
      srvConn2.Password = password  
      Dim srv3 As New Server(srvConn2)  
      Console.WriteLine(srv3.Information.Version)   ' connection is established  
   End Sub  
End Class  

在 Visual C 中使用 SQL Server 驗證連線到 SQL Server 實例#

當您使用 SQL Server 驗證連線到 SQL Server 實例時,您必須指定驗證類型。 這個範例示範宣告 ServerConnection 物件變數的替代方法,這可重複使用連接資訊。

此範例是 Visual C# .NET 程式代碼,示範如何連線到 remote 和 vPassword 包含登入和密碼。

// compile with:   
// /r:Microsoft.SqlServer.Smo.dll  
// /r:Microsoft.SqlServer.ConnectionInfo.dll  
// /r:Microsoft.SqlServer.Management.Sdk.Sfc.dll   
  
using System;  
using Microsoft.SqlServer.Management.Smo;  
using Microsoft.SqlServer.Management.Common;  
  
public class A {  
   public static void Main() {   
      String sqlServerLogin = "user_id";  
      String password = "pwd";  
      String instanceName = "instance_name";  
      String remoteSvrName = "remote_server_name";  
  
      // Connecting to an instance of SQL Server using SQL Server Authentication  
      Server srv1 = new Server();   // connects to default instance  
      srv1.ConnectionContext.LoginSecure = false;   // set to true for Windows Authentication  
      srv1.ConnectionContext.Login = sqlServerLogin;  
      srv1.ConnectionContext.Password = password;  
      Console.WriteLine(srv1.Information.Version);   // connection is established  
  
      // Connecting to a named instance of SQL Server with SQL Server Authentication using ServerConnection  
      ServerConnection srvConn = new ServerConnection();  
      srvConn.ServerInstance = @".\" + instanceName;   // connects to named instance  
      srvConn.LoginSecure = false;   // set to true for Windows Authentication  
      srvConn.Login = sqlServerLogin;  
      srvConn.Password = password;  
      Server srv2 = new Server(srvConn);  
      Console.WriteLine(srv2.Information.Version);   // connection is established  
  
      // For remote connection, remote server name / ServerInstance needs to be specified  
      ServerConnection srvConn2 = new ServerConnection(remoteSvrName);  
      srvConn2.LoginSecure = false;  
      srvConn2.Login = sqlServerLogin;  
      srvConn2.Password = password;  
      Server srv3 = new Server(srvConn2);  
      Console.WriteLine(srv3.Information.Version);   // connection is established  
   }  
}  

另請參閱

Server
ServerConnection