Handler 属性示例 (VB)

重要

从 Windows 8 和 Windows Server 2012 开始,Windows 操作系统不再包含 RDS 服务器组件(有关更多详细信息,请参阅 Windows 8 和 Windows Server 2012 兼容性实用手册)。 Windows 的未来版本中将移除 RDS 客户端组件。 请避免在新的开发工作中使用该功能,并着手修改当前还在使用该功能的应用程序。 使用 RDS 的应用程序应迁移到 WCF 数据服务

此示例演示 RDS DataControl 对象的 Handler 属性。 (有关详细信息,请参阅 DataFactory 自定义

假设参数文件 Msdfmap.ini 中的以下部分位于服务器上:

[connect AuthorDataBase]  
Access=ReadWrite  
Connect="DSN=Pubs"  
[sql AuthorById]  
SQL="SELECT * FROM Authors WHERE au_id = ?"  

代码如下所示。 分配给 SQL 属性的命令将与 AuthorById 标识符匹配,并将检索作者 Michael O'Leary 的行。 DataControl 对象的 Recordset 属性将分配给断开连接的 Recordset 对象(仅为编码方便起见)。

'BeginHandlerVB  
Public Sub Main()  
    On Error GoTo ErrorHandler  
  
    Dim dc As New DataControl  
    Dim rst As ADODB.Recordset  
  
    dc.Handler = "MSDFMAP.Handler"  
    dc.ExecuteOptions = 1  
    dc.FetchOptions = 1  
    dc.Server = "https://MyServer"  
    dc.Connect = "Data Source=AuthorDataBase"  
    dc.SQL = "AuthorById('267-41-2394')"  
    dc.Refresh                  'Retrieve the record  
    Set rst = dc.Recordset      'Use another Recordset as a convenience  
    Debug.Print "Author is '" & rst!au_fname & " " & rst!au_lname & "'"  
  
    ' clean up  
    If rst.State = adStateOpen Then rst.Close  
    Set rst = Nothing  
    Set dc = Nothing  
    Exit Sub  
  
ErrorHandler:  
    ' clean up  
    If Not rst Is Nothing Then  
        If rst.State = adStateOpen Then rst.Close  
    End If  
    Set rst = Nothing  
    Set dc = Nothing  
  
    If Err <> 0 Then  
        MsgBox Err.Source & "-->" & Err.Description, , "Error"  
    End If  
End Sub  
'EndHandlerVB  

另请参阅

DataControl 对象 (RDS)
Handler 属性 (RDS)