Sample Code for JS database operations

Source: Internet
Author: User

1. Create an SQL Server database
Database creation: test
Table creation:

Copy codeThe Code is 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 versions: 50520
File Encoding: 65001

Date: 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 ('2017-04-02 16:01:00 ', '2017-04-02 16:01:17', '0', 'oppo ', 'male ', '20 ');
Insert into [user] VALUES ('2017-04-02 16:01:02 ', '2017-04-02 16:01:17', '0', 'mini ', 'female ', '18 ');
Insert into [user] VALUES ('2017-04-02 16:01:04 ', '2017-04-02 16:01:17', '0', 'kina ', 'female ', '18 ');
Insert into [user] VALUES ('2017-04-02 16:01:06 ', '2017-04-02 16:01:17', '0', 'lora', 'male ', '19 ');
Insert into [user] VALUES ('2017-04-02 16:01:08 ', '2017-04-02 16:01:17', '0', 'rong Sir ', 'female ', '18 ');
Insert into [user] VALUES ('2017-04-02 16:01:10 ', '2017-04-02 16:01:17', '0', 'compatibility ', 'female ', '19 ');
Insert into [user] VALUES ('2017-04-02 16:01:13 ', '2017-04-02 16:01:17', '0', 'Sir cloud', 'male ', '18 ');
Insert into [user] VALUES ('2014-04-02 16:01:17 ', '2014-04-02 16:01:17', '0', '', 'male ', '18 ');
Insert into [user] VALUES ('2017-04-02 16:37:00 ', '2017-04-02 16:37:00', '0', 'guro Go Sir ', 'female ', '19 ');

2. Operate the database with JS Code on the page

Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> JS Database Operations </title>
<Script language = "javascript" type = "text/javascript">
Var conn, rs;

/* Obtain the database connection */
Function getConnection (){
Conn = new ActiveXObject ("ADODB. Connection ");
// 1. JavaScript operation database JS operation Access Database
// On disk F, there is a file abc. mdf with the table name "user". There are two fields in total, the id Primary Key and the name text type.
// Conn. Open ("DBQ = f: // abc. mdb; DRIVER = {Microsoft Access Driver (*. mdb )};");

// 2. JavaScript operations on the database JS operations on the SQL Server database
// Database name: test, table name: user, id: int type, auto-incrementing column, name: user name, varchar type, database username: sa, and password: sasa.
Conn. Open ("Driver = {SQL Server}; Server =.; DataBase = test; UID = sa; Password = sasa"); // Open the DataBase
Return conn;
}

/* Method for adding, deleting, and modifying operations */
Function executeUpdate (SQL ){
GetConnection ();
Try {
Conn.exe cute (SQL );
Return true;
} Catch (e ){
Document. write (e. description );
} Finally {
CloseAll ();
}
Return false;
}

/* Query Method */
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;
}
}

// Add
// ExecuteUpdate ("insert into [user] (create_date, edit_date, is_delete, [name], sex, age) VALUES ('2017-10-17 12:00:00 ', '2017-10-17 12:00:00 ', 0, 'kong', 'mal', 20 )");
// Delete
// ExecuteUpdate ("delete from [user] WHERE id = 1009 ");
// Modify
// ExecuteUpdate ("UPDATE [user] SET sex = 'femal', age = 18 WHERE id = 1009 ");
// Query
ExecuteQuery ("select * from [user]");
</Script>
</Head>

<Body>
</Body>
</Html>

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.