Role JavaScript access and manipulate databases

Source: Internet
Author: User

JS Operation Access Database

JS Code
  1. <script language="JavaScript" >
  2. <!--
  3. var filePath = location.href.substring (0, Location.href.indexOf ("instance 197. Connect to an Access database. html")); //Based on the current paging file, find the absolute path where the file resides
  4. var path = FilePath + "197.mdb";
  5. Path = path.substring (8);
  6. var objdbconn = new ActiveXObject ("ADODB.         Connection ");
  7. var strdsn = "Driver={microsoft Access driver (*.mdb)};d bq=" + path;
  8. Objdbconn.open (STRDSN);
  9. document.write ("Connection to Access database successfully!<br>");
  10. Objdbconn.close ();
  11. -
  12. </SCRIPT>

Connecting to a SQL Server database

JS Code
    1. <script language= "JavaScript" >  
    2. <!--  
    3. var objdbconn = new activexobject (" ADODB. Connection ");     
    4. var strdsn  =  "driver={sql server}; server= (local); Uid=sa; Pwd=111111;database=hljdatabase ";     
    5. objdbconn.open (STRDSN);        
    6. document.write (
    7. objdbconn.close ();                      
    8. //-->&NBSP;&NBSP;
    9. </script>  

Querying a DB instance

JS Code
  1. <script language="JavaScript" >
  2. <!--
  3. var objdbconn = new ActiveXObject ("ADODB.    Connection ");
  4. var strdsn = "Driver={sql Server}; server= (local); Uid=sa;      Pwd=111111;database=hljdatabase "; //need to modify your server address, username, password
  5. Objdbconn.open (STRDSN);
  6. var objRS = Objdbconn.execute ("select * FROM table where association = 0"); //Enter a local table
  7. var fdcount = objRS. fields.count-1;
  8. if (!objrs. EOF) {
  9. document.write ("<table border=1><tr>");
  10. For (var i=0; I <= fdcount; i++)
  11. document.write ("<td><b>" + objRS. Fields (i).  Name + "</b></td>");
  12. document.write ("</tr>");
  13. While (!objrs. EOF) {
  14. document.write ("<tr>");
  15. For (i=0; I <= fdcount; i++)
  16. document.write ("<td valign= ' top ' >" + objRS. Fields (i).  Value + "</td>");
  17. document.write ("</tr>");
  18. Objrs.movenext ();
  19. }
  20. document.write ("</table>");
  21. }
  22. Else
  23. document.write ("No records!<br> in the database");
  24. objRS. Close ();
  25. Objdbconn.close ();
  26. -
  27. </SCRIPT>

Manipulating Database instances

JS Code
  1. <script language="JavaScript" >
  2. <!--
  3. var objdbconn = new ActiveXObject ("ADODB.    Connection ");
  4. var strdsn = "Driver={sql Server}; server= (local); Uid=sa;   Pwd=111111;database=hljdatabase "; //need to modify your server address, username, password
  5. Objdbconn.open (STRDSN);
  6. Objdbconn.execute ("UPDATE table Set association = 0"); //write your own statement of execution
  7. document.write ("Update data successfully!<br>");
  8. Objdbconn.close ();
  9. -
  10. </SCRIPT>

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.