win7 win10 x64系统下的 vbs 连接 mdb 数据库方法

参考资料:https://www.cnblogs.com/liuzhaoyzz/p/6406629.html

今天用VBS读写MDB数据库,把原来在asp里面运行正常的代码拿过来,发现总是出现 x800a0e7a 错误:

查了一下,才想起来是因为64位系统未启用 32 位 MDB 数据库引擎造成的。

才回想起来当初 IIS7.5 里面跑 asp 的时候也出现过这个问题,改了下 IIS 设置允许32位程序倒是解决了。

但是 VBS 和 ASP 有点点不同,无法象 IIS 那样有个设置,怎么办呢?

又查了下,发现有网友解决过类似的问题:64位系统VBS调用32位COM组件(https://www.cnblogs.com/liuzhaoyzz/p/6406629.html),看起来是在使用 CreateObject 之前根据系统类型处理了一下 VBS 运行环境的设置,于是拿过来一用,发现也适用于 MDB 数据库引擎的创建,连接 MDB 数据库运行成功!

Option Explicit
call Run32()
dim conn,rs,rsB,connStr,sql,DBName
dim url1
dim apppath
	apppath=left(wscript.scriptfullname,instrrev(wscript.scriptfullname,"\")-1)	
	Set conn = CreateObject("ADODB.Connection")
	SET Rs = CreateObject("ADODB.Recordset")
	DBName=apppath & "\t1.mdb"
	connStr = "Provider = Microsoft.Jet.OLEDB.4.0;data source = " & DBName & ";Persist Security Info=False"	
	conn.Open connstr
	sql="Select * from zytq Where id=1"
	Set Rs=conn.Execute(sql)
	url1=Rs("url")
	call CloseDB(1)
	msgbox url1

Public Sub CloseDB(allClose)
    '1 ,AllClose & Nothing
    '0 ,AllClose
    '2 ,RS Close
    '3 ,RS Close & Nothing
    On Error Resume Next
    If allClose = 1 Then
        If Not RS Is Nothing Then
            If RS.State = 1 Then RS.Close
            Set RS = Nothing '----------------
        End If
        If Not conn Is Nothing Then
            If conn.State = 1 Then conn.Close
            Set conn = Nothing '---------------------
        End If
    ElseIf allClose = 0 Then
        If Not RS Is Nothing Then
            If RS.State = 1 Then RS.Close
        End If
        If Not conn Is Nothing Then
            If conn.State = 1 Then conn.Close
        End If
    ElseIf allClose = 2 Then
        If Not RS Is Nothing Then
            If RS.State = 1 Then RS.Close
        End If
    ElseIf allClose = 3 Then
        If Not RS Is Nothing Then
            If RS.State = 1 Then RS.Close
            Set RS = Nothing '--------------
        End If
    End If
    'Response.write "function1 CONN closeed!"
End Sub

为表谢意,Run32() 子程序我就不在这里贴出来了,需要的网友请前往 https://www.cnblogs.com/liuzhaoyzz/p/6406629.html 自行查看。

在此谢过!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章