- <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>