Posted in Javascript onOctober 17, 2013
一、创建SQL Server数据库
建库:test
建表:
/* 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', '男', '20'); INSERT INTO [user] VALUES ('2013-04-02 16:01:02', '2013-04-02 16:01:17', '0', 'Mini', '女', '18'); INSERT INTO [user] VALUES ('2013-04-02 16:01:04', '2013-04-02 16:01:17', '0', 'Kina', '女', '18'); INSERT INTO [user] VALUES ('2013-04-02 16:01:06', '2013-04-02 16:01:17', '0', 'Lora', '男', '19'); INSERT INTO [user] VALUES ('2013-04-02 16:01:08', '2013-04-02 16:01:17', '0', '榕Sir', '女', '18'); INSERT INTO [user] VALUES ('2013-04-02 16:01:10', '2013-04-02 16:01:17', '0', '兼容', '女', '19'); INSERT INTO [user] VALUES ('2013-04-02 16:01:13', '2013-04-02 16:01:17', '0', 'Sir云', '男', '18'); INSERT INTO [user] VALUES ('2013-04-02 16:01:17', '2013-04-02 16:01:17', '0', '明白了', '男', '18'); INSERT INTO [user] VALUES ('2013-04-02 16:37:00', '2013-04-02 16:37:00', '0', 'Guro Go Sir', '女', '19');
二、在页面JS代码操作数据库
<!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操作数据库</title> <script language="javascript" type="text/javascript"> var conn, rs; /*获取数据库连接*/ function getConnection() { conn = new ActiveXObject("ADODB.Connection"); // 1.JavaScript操作数据库JS操作Access数据库 // 在F盘有文件abc.mdf,表名为user,一共2个字段,id数字类型主键,name文本类型 // conn.Open("DBQ=f://abc.mdb;DRIVER={Microsoft Access Driver (*.mdb)};"); // 2.JavaScript操作数据库JS操作SQL Server数据库 // 数据库名为:test,表名为user,id为int类型,自增列,name为用户名,为varchar类型;数据库用户名为sa,密码是sasa。 conn.Open("Driver={SQL Server};Server=.;DataBase=test;UID=sa;Password=sasa"); //打开数据库 return conn; } /*执行增删改的方法*/ function executeUpdate(sql) { getConnection(); try { conn.execute(sql); return true; } catch (e) { document.write(e.description); } finally { closeAll(); } return false; } /*执行查询的方法*/ 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(); } } /*关闭所有资源*/ function closeAll() { if(rs != null) { rs.close(); rs = null; } if(conn != null) { conn.close(); conn = null; } } // 增 // 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, '空', '男', 20)"); // 删 // executeUpdate("DELETE FROM [user] WHERE id = 1009"); // 改 // executeUpdate("UPDATE [user] SET sex = '女', age = 18 WHERE id = 1009"); // 查 executeQuery("select * from [user]"); </script> </head> <body> </body> </html>
JS操作数据库的实例代码
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@