How to connect to the access database using javascript

Source: Internet
Author: User

I just saw a program written by a friend who used javascript to connect to the excel database and wanted to change it to an access database. I found these two articles.
------------------------------------------
Over the past few days, I have made great efforts to find the method for connecting javascript to the access database on the Internet,
Finally, a piece of code can be effectively executed.

However, this code can run normally locally. Once it is put in free personal space, it cannot be executed.
The main reason is that the Operation permission is insufficient. Most of the free space for static pages is supported.
Therefore, whether it is using mdb or reading/writing plain text files
But the page reports a js error. For free space outside China, mdb is directly prohibited.
Upload files with such extension.

Although you do not want to make your own counter or message board in the free space of the pure static page
This program may be useful to some friends and can be written for your research.

File structure:
The Access Database Name Is MyData. Mdb. A table named count is created in the Access database.
Field Composition: ID and COUNT. There is a piece of data in the table: ('Count', '123 ').
The text file name is count.txt, And a number is written in it.
The Static Page name is cnt.htm.
All three files are stored in the same directory.

Because the Access database is used and the non-standard SQL syntax is used, pay attention to
Square brackets must be added to the field name: []

The Code on the page is as follows:

The getCountFromDB method operates the Access database and getCountFromTxt
The method is to operate the txt plain text. Both methods are executed locally.

Copy codeThe Code is as follows: <! 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 page file, find the absolute path of the file.
Var filePath = location. href. substring (0, location. href. indexOf ("Cnt.htm "));
Var path = filePath + "MyData. mdb ";
// Remove the first "files: //" character in the string.
Path = path. substring (8 );
Var updateCnt = 0;

// Generate SQL statements for query and update.
Var sqlSelCnt = "Select count from [COUNT] Where ID = 'Count '";
Var sqlUpdCnt = "Update [COUNT] SET [COUNT] = '";

// Establish a connection and generate the relevant string www.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 );
// Add the obtained result to 1 and update the database.
UpdateCnt = cnt * 1 + 1;
Rs. moveNext;
}
Rs. close ();
Rs = null;

SqlUpdCnt = sqlUpdCnt + updateCnt + "'";
Con.exe cute (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 a text file in read-only mode.
Var ForReading = 1;
// Open a 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>

Note: although the free space that only supports static pages is unavailable
The free space of Asp should be usable. Of course I have not tried it, just as I think.
In addition, this method is used to read and write databases, and is only applicable to counters and message boards.
After all, the code is written in javascript and can be viewed by anyone,
Then you can get the path to download the db file for analysis, so you cannot use it.
Save things you don't want others to know.
As one of my colleagues said, using javascript to connect to the database for operations is simply
Overlord has a hard bow. Well, that's true. Since it cannot be used as your own plan in free space
The code can only be used for play, and you can find a good feeling.
-----------------------------
Many friends on the Internet are confused when solving some problems. Many people think that it is hard to connect to the database using javascript, and even conclude that javascript can only be executed by the client, I can't connect to the database, and I searched for it. The CSDN introduction is too general, so beginners cannot understand it at all. I didn't search for other forums. In this case, I decided to take a good look at MSDN. by reading the VC ++ database connection example, I summarized the method for connecting JAVASCRIPT to the database, which is now published to you.

Copy codeThe Code is as follows: <script language = javascript>

// ^_^

//

// (C) in 2002-2003 by Alex (Chinese: Jiang Jianhua)

// Http://www.finder.net.cn

// C Email: jiangjh@finder.net.cn

// F Email: jjh_115@eyou.com

</Script>

<Script language = javascript>

Function replace (v)

{

// Fault tolerance. Please make your own judgment.

// Define an SQL statement

Var SQL = "select * from Dictionary where MainID = '" + v + "'";

// Create a database connection object and a dataset Access Object

Var ConnDB = new ActiveXObject ("adodb. connection ");

Var rs = new ActiveXObject ("ADODB. Recordset ");

// Enter the DSN to be connected.

ConnDB. ConnectionString = "DSN = ***; uid = ***; pwd = ***";

ConnDB. open

Rs. open (SQL, ConnDB, 1, 1 );

If (rs. RecordCount> 0 ){

If (v = parseInt (rs ("MainID "))){

// String connection

Document. all ('txt '). value = v + rs ("MainValue ");

Var SQL = null;

Rs. close

ConnDB. close

Return;

} Else {

Var SQL = null;

Alert ("No such code! ");

Txt. select ();

Txt. focus ();

Rs. close

ConnDB. close

}

}

</Script>

<Input type = text id = txt name = txt onblur = "replace (this. value)">

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.