Javascript reads Access Database

Source: Internet
Author: User

These operations may not be used, but they are also posted. There are also many related examples on the Internet. I will not post a new article on my own. please correct me.

Under the directory where the HTML file is stored, there is a subdirectory for storing the database: webdata, where the database name is scdata. MDB
The database has a userlist table with two fields, userid and username, and a test data with userid = '20170101' and username = 'smallcol '.

JS Code:
File scengine. js
// Database-like connection pool
Function scdbpool (){
Try {
This. Con = new activexobject ("ADODB. Connection ");
This. Con. provider = "Microsoft. Jet. oledb.4.0 ";
This. rs = new activexobject ("ADODB. recordset ");
} Catch (e ){
This. Con = NULL;
This. rs = NULL;
This. filepath = NULL;
This. dbpath = NULL;

// Set the relative path of the database file (locate the file) and the database name
Scdbpool. Prototype. setdb = function (dbpath ){
This. dbpath = dbpath;

// Set the database location file. This step can be included in the connection class. Here, the database is written to facilitate the use of any name.
Scdbpool. Prototype. setdbpathposition = function (urlfile ){
VaR filepath = location. href. substring (0, location. href. indexof (urlfile ));
This. dbpath = (this. dbpath = NULL | this. dbpath = "")? "/Webdata/scdata. mdb": This. dbpath;
VaR Path = filepath + this. dbpath;
// Remove the "files: //" string before the path
This. filepath = path. substring (8 );

// Establish a connection with the database
Scdbpool. Prototype. Connect = function (){
This. Con. connectionstring = "Data Source =" + this. filepath;
This. Con. open;

// Execute the database statement and return the result set
Scdbpool.prototype.exe cutequery = function (SQL ){
This. Rs. Open (SQL, this. Con );

// No result set is returned when database statements are executed.
Scdbpool.prototype.exe cute = function (SQL ){
This.con.exe cute (SQL );

// Close the result set
Scdbpool. Prototype. rsclose = function (){
This. Rs. Close ();
This. rs = NULL;

// Close the data connection
Scdbpool. Prototype. conclose = function (){
This. Con. Close ();
This. Con = NULL;

HTML file. The page aa.html is the relative location file of the database.
<Script language = "JavaScript">
VaR DB = new scdbpool ();
DB. setdbpathposition ("aa.html ");
DB. Connect ();
VaR SQL = "select * from [userlist] Where userid = '000000 '";
Db.exe cutequery (SQL );
While (! DB. Rs. EOF ){
VaR CNT = dB. Rs. Fields ("username ");
Document. Write (CNT );
DB. Rs. movenext;
DB. rsclose ();
DB. conclose ();


The above is for my personal reference. please correct me if there is any error. Thank you. I am Continuously learning.

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: 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.