Ways to connect to an Access database with JavaScript

Source: Internet
Author: User
Tags count file upload connect sql string table name version access
access|javascript| Data | database

Choose a Blog from Kindwell

It's been a lot of effort these days. Find JavaScript on the web to connect to an Access database,
After finishing the summary, finally write a paragraph can be effectively executed code.

But this code works locally, and once placed in a free personal space, it can't be
OK, the main reason is that there is not enough operation permissions, because only static page support for free space most
is very restrictive, so whether it's in the form of an MDB or a plain text file
There is no way to pass, but by the page out of JS error. Some free space abroad, directly prohibit the MDB
File upload for this extension.

Although I do not want to be in the free space on the pure static page to make their own counter or message version, but
This procedure may be useful to some friends, write it for you to study.

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 stringwww.knowsky.com
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>

PostScript: Although free space is not available in support of static pages only, I would like some support
ASP free space should be able to use, of course, I did not specifically tried, but by their own imagination.
And in this way to read and write the database, only for the counter and the message version of this does not need
The work of confidentiality, after all, the code is written in JavaScript, who can see,
Then you can get the path to download the DB file for analysis, so there is no way to use it to
Keep something that you don't want to be known about.
As one of my colleagues said, using JavaScript to connect to a database is simply
Overlord the bow, oh, indeed, since can not be used in free space for their own account
Digital device, then this code can only be used to play, to find a good feeling.



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.