JavaScript uses ActiveXObject to Access and SQL Server databases, and javascriptactivex
JS operation Access Database
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var filePath = location. href. substring (0, location. href. indexOf ("Connecting accessdata warehouse .html"); // based on the current page file, locate the absolute path of the file
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 database connection successful! <Br> ");
ObjdbConn. Close ();
// -->
</SCRIPT>
Connect to the SQL Server database
Copy codeThe Code is as follows:
<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 ("database connection successful <br> ");
ObjdbConn. Close ();
// -->
</SCRIPT>
Query database instances
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var objdbConn = new ActiveXObject ("ADODB. Connection ");
Var strdsn = "Driver = {SQL Server}; SERVER = (local); UID = sa; PWD = 111111; DATABASE = hljdatabase"; // you need to modify your Server address, user name and password
ObjdbConn. Open (strdsn );
Var objrs = objdbConn. Execute ("SELECT * FROM table where association = 0"); // enter a local table
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 ("no records in the database! <Br> ");
Objrs. Close ();
ObjdbConn. Close ();
// -->
</SCRIPT>
Operate Database instances
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var objdbConn = new ActiveXObject ("ADODB. Connection ");
Var strdsn = "Driver = {SQL Server}; SERVER = (local); UID = sa; PWD = 111111; DATABASE = hljdatabase"; // you need to modify your Server address, user name and password
ObjdbConn. Open (strdsn );
ObjdbConn. Execute ("update table set Association = 0"); // write your own execution statement.
Document. write ("data update successful! <Br> ");
ObjdbConn. Close ();
// -->
</SCRIPT>