JS operation database instance code


**Create SQL Server database **Building: the test Build tables:

-- ----------------------------
-- 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 with ', ' 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', ' To understand the ', ' male ', '18');
INSERT INTO [user] VALUES ('2013-04-02 16:37:00', '2013-04-02 16:37:00', '0', 'Guro Go Sir', ' female ', '19');

Two, in the page JS code operation database

<!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 Operational database </title>
<script language="javascript" type="text/javascript">
var conn, rs;


function getConnection() {
    conn = new ActiveXObject("ADODB.Connection");
    //1.JavaScript operation database JS operation Access database
    //In the F disk there is the file abc.mdf, table name user, a total of 2 fields, id numeric type primary key, name text type
    // conn.Open("DBQ=f://abc.mdb;DRIVER={Microsoft Access Driver (*.mdb)};");

    //JavaScript operation database JS operation SQL Server database
    //Database name: test, table name: user, id: int, autoincrement, name: user name, varchar; The database user name is sa and the password is sasa.
    conn.Open("Driver={SQL Server};Server=.;DataBase=test;UID=sa;Password=sasa");       //Open database
    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;
    }
}

//  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)");
//  delete
// executeUpdate("DELETE FROM [user] WHERE id = 1009");
//  change
//ExecuteUpdate ("UPDATE [user] SET sex = 'female ', age = 18 WHERE id = 1009");
//  check
executeQuery("select * from [user]");
</script>
</head>

<body>
</body>
</html>