二、在脚本中添加代码调用数据库
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
Sub Main
Dim Result As Integer
'Initially Recorded: 2005-4-18 19:25:44
'Script Name: 调用数据库实例脚本
'SQL脚本
Dim connection ,i As long
Dim retcode As Variant
Dim outputstr,query as string
Dim out(1 To 1,1 to 4) As Variant
'通过ODBC连接到数据库,要先创建ODBC
connection = SQLOpen("DSN=LocalServer;UID=sa;PWD=sa", outputstr,)
'查询语句
query = "select * from LMS_COURSE where name_cn like 'HJ%'"
'执行查询
retcode = SQLExecQuery(connection,query)
'取回结果,结果存入二维数组
retcode = SQLRetrieve(connection,out())
'取回结果,如果结果存入文件请使用
retcode = SQLRetrieveToFile(connection,"c:\测试.xls")
'显示结果
msgbox out(1,1)
i=1
while i<3
'msgbox out(1,i)
i=i+1
wend
'关闭数据库连接
retcode = SQLClose(connection)
End Sub
注释部分就是对数据库的操作。
下一节讲一些和数据库操作相关函数的说明