連線到 SQL Server 的實例

適用於:SQL ServerAzure SQL DatabaseAzure SQL 受控執行個體Azure Synapse Analytics

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

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

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

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

  • 根據預設,會使用 Microsoft Windows 驗證來建立連線。 若要使用 SQL Server 驗證, LoginSecure 必須設定為 False,而且 LoginPassword 必須設定為有效的 SQL Server 登入和密碼。 安全性認證必須一律安全地儲存和處理,並盡可能在執行時間提供。

  • Connect必須先呼叫 方法,才能將連接傳遞至任何 RMO 程式設計物件。

範例

若要使用提供的任何程式碼範例,您必須選擇程式設計環境、程式設計範本,以及用來建立應用程式的程式設計語言。 如需詳細資訊,請參閱 在 Visual Studio .NET 中建立 Visual C# SMO 專案。

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

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

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

'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 的本機實例不需要太多程式碼。 相反地,它會依賴驗證方法和伺服器的預設設定。 需要擷取資料的第一個作業會導致建立連接。

此範例是使用 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 包含遠端實例的名稱。

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

此範例是使用 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