JavaScript uses ActiveXObject to Access and SQL Server databases, and javascriptactivex

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.