JavaScript USES ActiveXObject to access the Access and SQL Server databases

  • 2020-05-24 05:13:07
  • OfStack

JS operates on Access databases


<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var filePath = location.href.substring(0, location.href.indexOf(" The instance 197. The connection Access The database .html"));    // Based on the current page file, find the absolute path to 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(" The connection ACCESS Database successful !<br>"); 
objdbConn.Close();                         
//--> 
</SCRIPT> 

Connect to the SQL Server database


<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 the database instance


<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 , The user name , password  
objdbConn.Open(strdsn);      
var objrs = objdbConn.Execute("SELECT * FROM table where associated =0");                  // Enter the 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(" There are no records in the database !<br>"); 
objrs.Close();                          
objdbConn.Close();                    
//--> 
</SCRIPT> 

Operation database instance


<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 , The user name , password  
objdbConn.Open(strdsn);      
objdbConn.Execute("update table set associated =0");                    // Write your own execution statement  
document.write(" Update data successfully !<br>"); 
objdbConn.Close();                    
//--> 
</SCRIPT> 


Related articles: