共用方式為


連接到 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 物件會嘗試使用所有預設連接設定連接到預設的 SQL Server 實例。

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

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

連接共用

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

多執行緒應用程式

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

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

Replication Management Objects (RMO) 會使用與 SMO 稍微不同的方法來連接到複寫伺服器。

RMO 程式設計物件需要使用 命名空間所實作的物件,建立與 SQL Server ServerConnection 實例的連接 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 的本機實例不需要太多程式碼。 而是依賴驗證方法和伺服器的預設值。 需要擷取資料的第一個作業將會導致連接建立。

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

在 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 包含遠端實例的名稱。

在 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 Authentication 連線到 SQL Server 實例時,您必須指定驗證類型。 此範例示範宣告 ServerConnection 物件變數的替代方法,讓連接資訊得以重複使用。

範例是 Visual Basic .NET 程式碼,示範如何連線到遠端和 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 Authentication 連線到 SQL Server 實例時,您必須指定驗證類型。 此範例示範宣告 ServerConnection 物件變數的替代方法,讓連接資訊得以重複使用。

範例是 Visual C# .NET 程式碼,示範如何連線到遠端和 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