DB. ASP writes ASP with Javascript, which is flexible and easy to use. If you like to write asp with js, you can refer to it.
The Code is as follows:
<%
Function getConfig (config, args ){
If (args ){
For (var proto in args ){
Config [proto] = args [proto];
}
}
Return config;
}
Function getConnection (){
Return new ActiveXObject ("ADODB. Connection ");
}
Function getRecordset (){
Return new ActiveXObject ("ADODB. Recordset ");
}
Var DB = {};
DB. ConnectionString = 'provider = Sqloledb; User ID = sa; Password = sa; Initial Catalog = T; Data Source = WWW-D17F81FA113 \ SQLEXPRESS ;';
// Add a record
DB. Add = function (table, keyValueCol ){
Var returnID = null;
Var Conn = getConnection ();
Conn. Open (DB. ConnectionString );
Var Rs = getRecordset ();
Rs. Open ('select * from' + table + 'where 1 = 2', Conn, 3, 2 );
Rs. AddNew ();
For (var key in keyValueCol ){
Rs. Fields. Item (key). Value = keyValueCol [key];
}
Rs. Update ();
Rs. Close ();
Rs = null;
Conn. Close ();
Conn = null;
Return DB. Get ("select IDENT_CURRENT ('" + table + "') as ID") ["ID"];
}
// Modify a record
DB. Upd = function (SQL, keyValueCol ){
Var Conn = getConnection ();
Conn. Open (DB. ConnectionString );
Var Rs = getRecordset ();
Rs. Open (SQL, Conn, 3, 2 );
For (var key in keyValueCol ){
Rs. Fields. Item (key). Value = keyValueCol [key];
}
Rs. Update ();
Rs. Close ();
Rs = null;
Conn. Close ();
Conn = null;
}
// Execute a query with no returned results
DB. Exe = function (SQL ){
Var Conn = getConnection ();
Conn. Open (DB. ConnectionString );
Conn. Execute (SQL );
Conn. Close ();
Conn = null;
}
// Obtain a query record
DB. Get = function (SQL ){
Var _ record = null;
Var Conn = getConnection ();
Conn. Open (DB. ConnectionString );
Var Rs = getRecordset ();
Rs. Open (SQL, Conn, 1, 1 );
If (! Rs. EOF ){
_ Record = {};
For (var I = 0; I <Rs. Fields. Count; I ++ ){
_ Record [Rs. Fields. Item (I). Name] = Rs. Fields. Item (I). Value;
}
}
Rs. Close ();
Rs = null;
Conn. Close ();
Conn = null;
Return _ record;
}
// Obtain/add data in batches
DB. Batch = function (){
Var Conn = getConnection ();
Var Rs = getRecordset ();
Var _ Batch = this;
Var _ table = null;
_ Batch. Open = function (SQL ){
Conn. Open (DB. ConnectionString );
Rs. Open (SQL, Conn, 3, 2 );
}
_ Batch. Add = function (table, keyValueCol ){
Rs. AddNew ();
For (var key in keyValueCol ){
Rs. Fields. Item (key). Value = keyValueCol [key];
}
Rs. Update ();
Return DB. Get ("Select IDENT_CURRENT ('" + table + "') as ID") ["ID"];
}
_ Batch. Get = function (){
Var record_arr = [];
While (! Rs. EOF ){
Var _ record = {};
For (var I = 0; I <Rs. Fields. Count; I ++ ){
_ Record [Rs. Fields. Item (I). Name] = Rs. Fields. Item (I). Value;
}
Record_arr.push (_ record );
Rs. MoveNext ();
}
Return record_arr;
}
_ Batch. Close = function (){
Rs. Close ();
Rs = null;
Conn. Close ();
Conn = null;
}
}
// Obtain data on a page of SQL
DB. List = function (){
Var _ Config;
Var _ List = this;
_ List. Page = {
PS: 20,
AP: 1,
PC: 1,
RC: 1
};
_ List. Query = function (){
_ Config = new getConfig ({
Fields :"*",
Table: null,
Where: "1 = 1 ",
Sort: "ID desc ",
Pk: "ID"
}, Arguments [0]);
_ List. Page. RC = DB. Get ("select count (" + _ Config. pk + ") as [count] from" +
_ Config. table + "where" + _ Config. where). count;
_ List. Page. PC = Math. ceil (_ List. Page. RC/_ List. Page. PS );
If (_ List. Page. AP> _ List. Page. PC) _ List. Page. AP = _ List. Page. PC;
}
_ List. Get = function (p ){
P = isNaN (p )? 1: parseInt (p );
_ List. Page. AP = p;
Var SQL = '';
If (p> 1 ){
SQL = "select top" + _ List. Page. PS + "" + _ Config. fields +
"From" + _ Config. table + "where" + _ Config. where +
"And" + _ Config. pk +
"Not in (select top" + (p-1) * _ List. Page. PS + "" + _ Config. pk +
"From" + _ Config. table + "where" + _ Config. where +
"Order by" + _ Config. sort + ") order by" + _ Config. sort;
} Else {
SQL = "select top" + _ List. Page. PS + "" + _ Config. fields +
"From" + _ Config. table + "where" + _ Config. where + "order by" + _ Config. sort;
}
Var return_arr = null;
Var Batch = new DB. Batch ();
Batch. Open (SQL );
Return_arr = Batch. Get ();
Batch. Close ();
Return return_arr;
}
}
// SQL read-only
DB. Reader = function (){
Var Conn = getConnection ();
Var Rs = getRecordset ();
Var _ Reader = this;
_ Reader. EOF = false;
_ Reader. Open = function (SQL ){
Conn. Open (DB. ConnectionString );
Rs. Open (SQL, Conn, 1, 1 );
_ Reader. EOF = Rs. EOF;
}
_ Reader. Read = function (){
If (! Rs. EOF ){
Var _ record = {};
For (var I = 0; I <Rs. Fields. Count; I ++ ){
_ Record [Rs. Fields. Item (I). Name] = Rs. Fields. Item (I). Value;
}
Rs. MoveNext ();
Return _ record;
} Else {
_ Reader. EOF = true;
}
}
_ Reader. Close = function (){
Rs. Close ();
Rs = null;
Conn. Close ();
Conn = null;
}
}
%>