VBScript和JavaScript连接数据库(ACCESS)

1.VBScript连接数据库

程序代码 程序代码
<script language="vbscript">
Dim conn,rs,sqlstr
Set conn = CreateObject("ADODB.Connection")
conn.connectionstring = "provider=microsoft.jet.oledb.4.0;data source=F:/wwroot/mzwu/db1.mdb"
conn.open
Set rs = CreateObject("ADODB.RecordSet")
sqlstr = "Select top 1 * from table1"
rs.open sqlstr,conn,1,2
document.write rs(0) & " " & rs(1)
rs.close
Set rs = nothing
conn.close
Set conn = nothing
</script>

说明:VBScript和ASP连接数据库的不同之处仅在于创建ADO对象的方式不同而已,其他操作都一样。

2.JavaScript连接数据库
程序代码 程序代码
<script language="javascript">
var conn,rs,sqlstr;
conn = new ActiveXObject("ADODB.Connection");
conn.connectionstring = "provider=microsoft.jet.oledb.4.0;data source=F:/wwroot/mzwu/db1.mdb";
conn.open;
rs = new ActiveXObject("ADODB.RecordSet");
sqlstr = "Select top 1 * from [table1]";
rs.open(sqlstr,conn,1,2);
document.write(rs(0) + " " + rs(1));
rs.close
rs = null;
conn.close
conn = null;
</script>

说明:和ASP连接数据库相比,JavaScript在创建ADO对象、SQL书写格式以及释放对象上都有些不同,要注意体会。

<script language=vbscript>
 set conn = createobject("adodb.connection")
 ConStr="Driver={Microsoft Access Driver (*.mdb)};dbq=C:\Users\lenovo\Desktop\db1.mdb"
 conn.open ConStr
 set rs = createobject("adodb.recordset")
 rs.open "select * from log1 where id=1",conn
 alert("id="&rs(0)&"name="&rs(1))
</script>




你可能感兴趣的:(VBScript和JavaScript连接数据库(ACCESS))