连接到 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 对象。

连接到用于 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 编程对象之前,必须调用该方法。

例子

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