Db. ASP used JavaScript to write ASP is very flexible and easy to use very easy_javascript skills

Source: Internet
Author: User
Copy Code code 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 without returning results
Db. Exe = function (sql) {
var Conn = getconnection ();
Conn.Open (DB. ConnectionString);
Conn.execute (SQL);
Conn.close ();
Conn = null;
}
Get 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;
}
Bulk Get/Add data
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;
}
}
Get data for a page in 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;
}
}
%>
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.