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>