The example code _javascript skill of JS operation database

Source: Internet
Author: User

First, create a SQL Server database
Building a library: test
Build table:

Copy Code code as follows:

/*
Navicat MySQL Data Transfer

Source Server:localhost
Source Server version:50520
Source host:localhost:3306
Source Database:mo

Target Server Type:mysql
Target Server version:50520
File encoding:65001

DATE:2013-10-17 12:00:56
*/

-- ----------------------------
--Table structure for [user]
-- ----------------------------
Use test
Go

DROP TABLE [user];
CREATE TABLE [user] (
ID bigint not NULL PRIMARY KEY IDENTITY (1000, 1),
create_date datetime DEFAULT NULL,
edit_date datetime DEFAULT NULL,
is_delete int DEFAULT NULL,
[Name] varchar (255) DEFAULT NULL,
Sex varchar (255) DEFAULT NULL,
Age int DEFAULT NULL
);

-- ----------------------------
--Records of user
-- ----------------------------
INSERT into [user] VALUES (' 2013-04-02 16:01:00 ', ' 2013-04-02 16:01:17 ', ' 0 ', ' Oppo ', ' Male ', ' 20 ');
INSERT into [user] VALUES (' 2013-04-02 16:01:02 ', ' 2013-04-02 16:01:17 ', ' 0 ', ' Mini ', ' female ', ' 18 ');
INSERT into [user] VALUES (' 2013-04-02 16:01:04 ', ' 2013-04-02 16:01:17 ', ' 0 ', ' kina ', ' female ', ' 18 ');
INSERT into [user] VALUES (' 2013-04-02 16:01:06 ', ' 2013-04-02 16:01:17 ', ' 0 ', ' Lora ', ' Male ', ' 19 ');
INSERT into [user] VALUES (' 2013-04-02 16:01:08 ', ' 2013-04-02 16:01:17 ', ' 0 ', ' Banyan Sir ', ' female ', ' 18 ');
INSERT into [user] VALUES (' 2013-04-02 16:01:10 ', ' 2013-04-02 16:01:17 ', ' 0 ', ' compatible ', ' female ', ' 19 ');
INSERT into [user] VALUES (' 2013-04-02 16:01:13 ', ' 2013-04-02 16:01:17 ', ' 0 ', ' Sir Cloud ', ' Male ', ' 18 ');
INSERT into [user] VALUES (' 2013-04-02 16:01:17 ', ' 2013-04-02 16:01:17 ', ' 0 ', ' understood ', ' male ', ' 18 ');
INSERT into [user] VALUES (' 2013-04-02 16:37:00 ', ' 2013-04-02 16:37:00 ', ' 0 ', ' guro go Sir ', ' female ', ' 19 ');

Second, in the page JS code operation database

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
&LT;TITLE&GT;JS Operations Database </title>
<script language= "javascript" type= "Text/javascript" >
var conn, RS;

/* Get the database connection * *
function getconnection () {
conn = new ActiveXObject ("ADODB. Connection ");
1.JavaScript Operation Database JS operation Access database
There are file abc.mdf on the F disk, table name is User, total 2 fields, ID number type primary key, name text type
Conn. Open ("Dbq=f://abc.mdb;driver={microsoft Access DRIVER (*.mdb)};");

2.JavaScript operation database JS operation SQL Server database
The database name is: Test, the table name is User,id int type, self added, name is username, varchar type, database user name SA, password is sasa.
Conn. Open ("Driver={sql Server}"; server=.;D Atabase=test; Uid=sa;       Password=sasa "); Open Database
Return conn;
}

* * To implement the method of adding or deleting changes
function executeupdate (SQL) {
Getconnection ();
try {
Conn.execute (SQL);
return true;
catch (e) {
document.write (e.description);
finally {
CloseAll ();
}
return false;
}

/* Method of executing the query * *
function executequery (SQL) {
Getconnection ();
try {
rs = new ActiveXObject ("ADODB.") Recordset ");
Rs.Open (SQL, conn);
var html = "";
while (!rs. EOF) {
HTML = html + rs. Fields ("id") + "" + Rs. Fields ("name") + "<br/>";
Rs.movenext ();
}
document.write (HTML);
catch (e) {
document.write (e.description);
finally {
CloseAll ();
}
}

/* Close all resources * *
function CloseAll () {
if (Rs!= null) {
Rs.close ();
rs = null;
}
IF (conn!= null) {
Conn.close ();
conn = null;
}
}

Increase
Executeupdate ("INSERT into [user] (Create_date, Edit_date, Is_delete, [name], sex, age) VALUES (' 2013-10-17 12:00:00 ', ' 2013-10-17 12:00:00 ', 0, ' empty ', ' male ', 20 ');
By deleting
Executeupdate ("DELETE from [user] WHERE id = 1009");
Change
Executeupdate ("UPDATE [user] SET sex = ' female ', age = the WHERE id = 1009");
Check
ExecuteQuery ("SELECT * from [user]");
</script>

<body>
</body>

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.