JScript uses ADO to operate databases

Source: Internet
Author: User

Idea: Call the object through the new activexobject () method in JScript

 

// Demo 1 reads data

Function readfromdb ()
{
VaR S = "";
// Create a database object
VaR objdbconn = new activexobject ("ADODB. Connection ");
// DSN string
// Var strdsn = "driver = {SQL Server}; server = 127.0.0.1; database = study; uid = sa; Pwd ="; // SQL server connection method
// Var strdsn = "provider = Microsoft. Ace. oledb.12.0; Data Source = c: \ database1.accdb; persist Security info = false"; // Office 2007 access
VaR strdsn = "provider = Microsoft. Jet. oledb.4.0; Data Source = c: \ database1.mdb; persist Security info = false"; // Office 2003 access
// Open the data source
Objdbconn. Open (strdsn );
// SQL-executed Database Query
VaR objrs = objdbconn. Execute ("select * from DB ");
// Obtain the number of fields
VaR fdcount = objrs. Fields. Count-1;
// Check for records
If (! Objrs. EOF ){
S = "<Table border = 1> <tr> ";
// Display the database field name
For (VAR I = 0; I <= fdcount; I ++)
{
S + = "<TD> <B>" + objrs. Fields (I). Name + "</B> </TD> ";
}
S + = "</tr> ";
// Display the database content
While (! Objrs. EOF ){
S + = "<tr> ";
// Display the fields of each record
For (I = 0; I <= fdcount; I ++)
{
S + = "<TD valign = 'top'>" + objrs. Fields (I). Value + "</TD> ";
}
S + = "</tr> ";
Objrs. movenext (); // move to the next record
}
S + = "</table> ";
}
Else
// Document. Write ("no records in the database! <Br/> ");
S = "no records in the database! <Br/> ";
Objrs. Close (); // close the record set
Objdbconn. Close (); // close the database link

Document. getelementbyid ("show"). innerhtml = s;
}

 

// DEMO 2 insert data

Function insertdb ()
{
VaR objdbconn = new activexobject ("ADODB. Connection ");
VaR strdsn = "provider = Microsoft. Jet. oledb.4.0; Data Source = c: \ database1.mdb; persist Security info = false"; // Office 2003 access
Objdbconn. Open (strdsn );
VaR SQL = "insert into dB (username, userage) values ('aaa', 25 )";
Objdbconn. Execute (SQL); // update
Objdbconn. Close ();
 
 
// Show
Readfromdb ();
}

 

// demo 3 insert data 2

function insertdb2 ()
{< br> // note that recordset is used to update data. You must change the Access File Permission to everyone for read and write,
// The execute method of the connection object is not required.
var objdbconn = new activexobject ("ADODB. connection ");
var strdsn =" provider = Microsoft. jet. oledb.4.0; Data Source = c: \ database1.mdb; persist Security info = false "; // Office 2003 access
var SQL =" select * from DB ";
var rs = new activexobject ("ADODB. recordset ");
objdbconn. open (strdsn);
Rs. open (SQL, objdbconn,);
// Insert the last row
Rs. movelast ();
Rs. addnew ();
RS ("username") = "BBB";
RS ("userage") = 29;
Rs. update ();
Rs. close ();
objdbconn. close ();
// show
readfromdb ();
}

 

 

// Demo 4 insert data 3

Function insertdb3 ()
{
VaR objdbconn = new activexobject ("ADODB. Connection ");
VaR strdsn = "provider = Microsoft. Jet. oledb.4.0; Data Source = c: \ database1.mdb; persist Security info = false"; // Office 2003 access
Objdbconn. Open (strdsn );

VaR cmd = new activexobject ("ADODB. Command ");
Cmd. activeconnection = objdbconn;
Cmd. commandtext = "insert into dB (username, userage) values (: username,: userage )";

VaR para1 = new activexobject ("ADODB. parameter ");
Para1.type = adbstr;
Para1.direction = adparaminput;
Para1.name = "username"
Para1.value = "ansy ";

VaR para2 = new activexobject ("ADODB. parameter ");
Para2.type = adinteger;
Para2.direction = adparaminput;
Para2.name = "userage"
Para2.value = 35;

Cmd. Parameters. append (para1 );
Cmd. Parameters. append (para2 );


Cmd. Execute ();

// Show
Readfromdb ();
}

 

Description

1. Demo 4 requires constants in ADO. Click here to download adojavas. Inc.

2. for programming, see ADO. CHM.

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.