<p>使用前安装客户端程序,设置简单,可以联系SQL语句</p><p>Public Sub myors()<br/> Dim cnn As New ADODB.Connection<br/> Dim rs As New ADODB.Recordset<br/> Dim cnnStr As String, myTable As String, SQL As String, i As Integer<br/> 'myTable = "BOOK_STOCK" '指定数据表<br/> '设置建立与Oracle数据库服务器中连接的字符串<br/> <br/> Range("A3:G2000").Select '以下两行是清除数据Contents<br/> Range("A3:G2000").ClearContents<br/> <br/> Lbname1 = Trim(Cells(2, 1))<br/> Lbname2 = Trim(Cells(2, 2))<br/> Lbname3 = Trim(Cells(2, 3))<br/> SQLname = Trim(Cells(2, 4))<br/> <br/> cnnStr = "Provider=MSDAORA;" _<br/> & "Data Source=" & Lbname1 & ";" _<br/> & "User ID=" & Lbname2 & ";" _<br/> & "Password=" & Lbname3 & ";"<br/> <br/> 'oadb是oracle服务器名;<br/> cnn.ConnectionString = cnnStr<br/> cnn.Open<br/> '创建查询记录集<br/> SQL = SQLname<br/> 'SQL = "select * from " & myTable<br/> 'SQL = "select * from " & myTable & " where BOOK_PUBLISHER='中国电力出版社'"<br/> MsgBox SQL<br/> <br/> rs.CursorLocation = adUseClient '游标改为客户端游标<br/> <br/> rs.Open Source:=SQL, ActiveConnection:=cnn<br/> <br/> With rs<br/> For i = 1 To .Fields.Count '复制字段名<br/> Cells(3, i) = .Fields(i - 1).Name<br/> <br/> Next i<br/> MsgBox "纪录数:" & rs.RecordCount<br/> Range("A4").CopyFromRecordset rs '复制记录数据<br/> .Close '关闭记录集<br/> End With<br/> cnn.Close<br/> Set rs = Nothing<br/> Set cnn = Nothing<br/> Range("A3").Select<br/>End Sub</p><p> 注:参考了《EXCEL VBA整合数据库应用》一书,但发现:</p><p> For i = 1 To .Fields.Count '复制字段名<br/> Cells(3, i) = .Fields(i - 1).Name<br/> <br/> Next i<br/>这段代码有错误,原来: Cells(3, i) = .Fields(i - 1)</p><p>更改为: Cells(3, i) = .Fields(i - 1).Name<br/><br/></p>
aYKj1Bvz.rar
(23 KB, 下载次数: 509)
<br/>感觉调用oracle数据库数据速度很快,可以作为人力资源统计数据来用。
[此贴子已经被作者于2008-4-2 9:55:26编辑过] |