DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JS操作數據庫的實例代碼
JS操作數據庫的實例代碼
編輯:關於JavaScript     

一、創建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>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved