连接到 SQL Server 实例

适用于:SQL ServerAzure SQL 数据库Azure 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 对象尝试使用所有默认连接设置连接到 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由 Microsoft.SqlServer.Management.Common 命名空间实现的对象建立到 SQL Server 实例的连接。 与服务器的这种连接独立于 RMO 编程对象进行。 然后,它会在创建实例期间或通过分配给 ConnectionContext 对象的属性传递给 RMO 对象。 通过这种方式,可以单独创建和管理 RMO 编程对象对象和连接对象实例,并且可以与多个 RMO 编程对象重复使用单个连接对象。 以下规则适用于与复制服务器的连接:

  • 为指定 ServerConnection 对象定义连接的所有属性。

  • 与 SQL Server 实例的每个连接都必须有自己的 ServerConnection 对象。

  • 在对象中 ServerConnection 提供了用于建立连接并成功登录服务器的所有身份验证信息。

  • 默认情况下,使用 Microsoft Windows 身份验证建立连接。 若要使用 SQL Server 身份验证, LoginSecure 必须设置为 False, Login 并且 Password 必须设置为有效的 SQL Server 登录和密码。 安全凭据必须始终安全地存储和处理,并尽可能在运行时提供。

  • Connect在将连接传递给任何 RMO 编程对象之前,必须调用该方法。

示例

若要使用提供的任何代码示例,必须选择要在其中创建应用程序的编程环境、编程模板和编程语言。 有关详细信息,请参阅 在 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 包含登录和密码。

' 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 代码,演示如何连接到远程和 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