Source 属性范例

该范例通过打开三个基于不同数据源的 Recordset 对象演示 Source 属性。

Public Sub SourceX()   Dim cnn1 As ADODB.Connection
   Dim rstTitles As ADODB.Recordset
   Dim rstPublishers As ADODB.Recordset
   Dim rstPublishersDirect As ADODB.Recordset
   Dim rstTitlesPublishers As ADODB.Recordset
   Dim cmdSQL As ADODB.Command
   Dim strCnn As String
   Dim strSQL As String   ' 打开连接。
   Set cnn1 = New ADODB.Connection
   strCnn = "Provider=sqloledb;" & _
      "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
   cnn1.Open strCnn
   
   ' 打开基于命令对象的记录集。
   Set cmdSQL = New ADODB.Command
   Set cmdSQL.ActiveConnection = cnn1
   cmdSQL.CommandText = "Select title, type, pubdate " & _
      "FROM titles ORDER BY title"
   Set rstTitles = cmdSQL.Execute()
   
   ' 打开基于表的记录集。
   Set rstPublishers = New ADODB.Recordset
   rstPublishers.Open "publishers", strCnn, , , adCmdTable
   
   ' 打开基于表的记录集。
   Set rstPublishersDirect = New ADODB.Recordset
   rstPublishersDirect.Open "publishers", strCnn, , , adCmdTableDirect
      ' 打开基于 SQL 字符串的记录集。
   Set rstTitlesPublishers = New ADODB.Recordset
   strSQL = "SELECT title_ID AS TitleID, title AS Title, " & _
      "publishers.pub_id AS PubID, pub_name AS PubName " & _
      "FROM publishers INNER JOIN titles " & _
      "ON publishers.pub_id = titles.pub_id " & _
      "ORDER BY Title"
   rstTitlesPublishers.Open strSQL, strCnn, , , adCmdText   ' 使用 Source 属性显示每个记录集的资源。
   MsgBox "rstTitles source: " & vbCr & _
      rstTitles.Source & vbCr & vbCr & _
      "rstPublishers source: " & vbCr & _
      rstPublishers.Source & vbCr & vbCr & _
      "rstPublishersDirect source: " & vbCr & _
      rstPublishersDirect.Source & vbCr & vbCr & _
      "rstTitlesPublishers source: " & vbCr & _
      rstTitlesPublishers.Source   rstTitles.Close
   rstPublishers.Close
   rstTitlesPublishers.Close
   cnn1.CloseEnd Sub
www.51windows.Net