JavaScript connects to an Access database

Source: Internet
Author: User
access|javascript| Data | Database file composition:
The Access database is named Mydata.mdb, and a table named Count is built inside the table, which consists of two
Field consists of: ID and Count, and a table with data: (' count ', ' 100 ').
The text file is named Count.txt, and a number is written inside.
The static page name is cnt.htm.
All 3 files are placed in the same directory.

Because it takes an Access database and it takes a non-standard SQL syntax, be aware that the table name
and field names need to be added square brackets: []

Here is the code in the page:

Where the Getcountfromdb method is to manipulate the Access database, Getcountfromtxt
The method is to operate on txt plain text, both of which are executed locally.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta name= "generator" content= "EditPlus" >
<meta name= "Author" content= "" >
<meta name= "Keywords" content= "" >
<meta name= "Description" content= "" >
<script language= "JavaScript" >
<!--
function Getcountfromdb () {
Based on the current paging file, locate the absolute path where the file is located.
var filePath = location.href.substring (0, Location.href.indexOf ("cnt.htm"));
var path = FilePath + "Mydata.mdb";
Remove the 8 characters from the first "files://" in the string.
Path = path.substring (8);
var updatecnt = 0;

The SQL statement that is used to generate the query and update.
var sqlselcnt = "SELECT count from [Count] WHERE ID = ' count '";
var sqlupdcnt = "UPDATE [Count] SET [count] = '";

Establish a connection and generate a related string www.zhangpeng.com.cn
var con = new ActiveXObject ("ADODB.") Connection ");
Con. Provider = "Microsoft.Jet.OLEDB.4.0";
Con. ConnectionString = "Data source=" + path;

Con.open;
var rs = new ActiveXObject ("ADODB. Recordset ");
Rs.Open (sqlselcnt, con);
while (!rs.eof) {
var cnt = Rs. Fields ("COUNT");
document.write (CNT);
The database will be updated after the results are obtained plus 1.
UPDATECNT = CNT * 1 + 1;
Rs.movenext;
}
Rs.close ();
rs = null;

sqlupdcnt = sqlupdcnt + updatecnt + "'";
Con.execute (SQLUPDCNT);

Con.close ();
con = null;
}

function Getcountfromtxt () {
var filePath = location.href.substring (0, Location.href.indexOf ("cnt.htm"));
var path = FilePath + "Count.txt";
Path = path.substring (8);
var nextcnt = 0;

Var fso, F1, TS, s;
Open the text file as read-only.
var ForReading = 1;
Open the text file in read-write mode.
var forwriting = 2;

FSO = new ActiveXObject ("Scripting.FileSystemObject");
F1 = fso. GetFile (path);
TS = F1. OpenAsTextStream (ForReading, true);
s = ts. ReadLine ();
nextcnt = eval (s) + 1;
document.write ("Now count is:" + s);
Ts. Close ();

TS = F1. OpenAsTextStream (ForWriting, true);
Ts. WriteLine (NEXTCNT);
Ts.close ();
}
-->
</SCRIPT>
</HEAD>

<BODY>
<script language= "JavaScript" >
<!--
Getcountfromtxt ();
-->
</SCRIPT>
</BODY>
</HTML>

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.