JavaScript使用ActiveXObject访问Access和SQL Server数据库
JS操作 Access 数据库
代码如下:
<SCRIPT LANGUAGE="JavaScript"> <!-- var filePath = location.href.substring(0, location.href.indexOf("实例197.连接Access数据库.html")); //以当前页面文件为基础,找到文件所在的绝对路径 var path = filePath + "197.mdb"; path = path.substring(8); var objdbConn = new ActiveXObject("ADODB.Connection"); var strdsn = "driver={Microsoft Access Driver (*.mdb)};dbq=" + path; objdbConn.Open(strdsn); document.write("连接ACCESS数据库成功!<br>"); objdbConn.Close(); //--> </SCRIPT>
连接SQL Server数据库
代码如下:
<SCRIPT LANGUAGE="JavaScript"> <!-- var objdbConn = new ActiveXObject("ADODB.Connection"); var strdsn = "Driver={SQL Server};SERVER=(local);UID=sa;PWD=111111;DATABASE=hljdatabase"; objdbConn.Open(strdsn); document.write("数据库连接成功<br>"); objdbConn.Close(); //--> </SCRIPT>
查询数据库实例
代码如下:
<SCRIPT LANGUAGE="JavaScript"> <!-- var objdbConn = new ActiveXObject("ADODB.Connection"); var strdsn = "Driver={SQL Server};SERVER=(local);UID=sa;PWD=111111;DATABASE=hljdatabase"; // 需要修改自己的服务器地址,用户名,密码 objdbConn.Open(strdsn); var objrs = objdbConn.Execute("SELECT * FROM 表 where 关联=0"); // 输入本地的表 var fdCount = objrs.Fields.Count - 1; if (!objrs.EOF){ document.write("<table border=1><tr>"); for (var i=0; i <= fdCount; i++) document.write("<td><b>" + objrs.Fields(i).Name + "</b></td>"); document.write("</tr>"); while (!objrs.EOF){ document.write("<tr>"); for (i=0; i <= fdCount; i++) document.write("<td valign='top'>" + objrs.Fields(i).Value + "</td>"); document.write("</tr>"); objrs.moveNext(); } document.write("</table>"); } else document.write("数据库内没有记录!<br>"); objrs.Close(); objdbConn.Close(); //--> </SCRIPT>
操作数据库实例
代码如下:
<SCRIPT LANGUAGE="JavaScript"> <!-- var objdbConn = new ActiveXObject("ADODB.Connection"); var strdsn = "Driver={SQL Server};SERVER=(local);UID=sa;PWD=111111;DATABASE=hljdatabase"; // 需要修改自己的服务器地址,用户名,密码 objdbConn.Open(strdsn); objdbConn.Execute("update 表 set 关联=0"); // 请写入自己的执行语句 document.write("更新数据成功!<br>"); objdbConn.Close(); //--> </SCRIPT>
相关推荐
风之羽翼 2020-09-14
daillo 2020-04-20
踩风火轮的乌龟 2020-04-19
hithyc 2020-04-17
snowguy 2020-04-10
yangliuhbhd 2020-09-24
luotuofeile 2020-07-28
Strongding 2020-06-25
xuanwenchao 2020-06-14
昊 2020-06-04
ysmh00 2020-05-14
ErixHao 2020-05-08
数据库之扑朔迷离 2020-05-06
whyname 2020-04-29
hitxueliang 2020-04-18
zengfanpei 2020-03-08
阿亮 2020-01-30