ASP既可以用vbscript写,也可以用javascript来写。就目前我研究的情况,javascript相对vbscript唯有类的销毁函数无法支持,其它的都完全支持,包括无组件上传。而javascript好些有用的功能,比如prototype、不定个数参数等vbscript就无法支持。
对于javascript读取数据库的情况,完全可以仿照vbscript中的写法,只是在函数调用时加上括号即可。
简单一下:
查看源代码打印帮助1 var connection = new ActiveXObject("ADODB.Connection");//相当于Server.CreateObject,都是微软的东东
2 connection.cursorLocation = 3; //这个是为了在执行sql语句时用connect.Execute ,而不是繁琐的RecordSet
3 connection.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath('db.mdb'));
4 var rs = connection.Execute("Select * From [users]");
下面有一个完整经过封闭的javascript数据库类。
查看源代码打印帮助001 <%
002 //数据库类
003
004 function Database(){this.initalize.apply(this, arguments);}
005
006 Database.prototype = {
007
008 connection : null?? ?//连接
009 ,record_set : null?? ?//记录集
010
011 //初始化
012 ,initalize : function(filepath){
013 //全局只使用一个连接
014 if(!this.connection){
015 this.connection = Server.CreateObject("ADODB.Connection");
016 this.connect(filepath);
017 this.record_set = Server.CreateObject("ADODB.Recordset");
018 this.record_set.ActiveConnection = this.connection;
019 this.record_set.CursorType = 1;
020 }
021 return true;
022 }
023
024 //连接
025 ,connect : function(filePath){
026 this.connection.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +filePath);
027 }
028
029 //执行
030 ,execute : function(sql){
031 return this.connection.Execute(sql);
032 }
033 //查询
034 ,query : function(sql){
035 try{this.record_set.Open(sql);}catch(e){die(sql)}
036 }
037
038 //获取记录行数
039 ,count : function(){
040 return this.record_set.recordCount;
041 }
042
043 //获取二维数组
044 ,get_rows : function(sql){
045 var i, rows = [], row = [], rs = this.record_set;
046 this.query(sql);
047 while(!rs.eof){
048 row = [];
049 for(i=0; i
050 row[rs.fields(i).name + ''] = rs.fields(i).value + '';
051 }
052 rows.push(row);
053 this.record_set.MoveNext();
054 }
055 this.record_set.Close();
056 return rows;
057 }
058
059 //获取一行数组
060 ,get_row : function(sql){
061 var i, row = [];
062 this.query(sql);
063 if(!this.record_set.eof){
064 for(i=0; i
065 //row.push = this.record_set(i) + '';
066 row[this.record_set.fields(i).Name + ''
] = this.record_set.fields(i).Value + '';
067 }
068 }
069 this.record_set.Close();
070 return row;
071 }
072
073 //获取一个数据
074 ,get_var : function(sql){
075 var r = null;
076 this.query(sql);
077 if(!this.record_set.eof){
078 r = this.record_set.fields(0).Value + '';
079 }
080 this.record_set.Close();
081 return r;
082 }
083
084 //向表插入数据
085 ,insert : function (table, values){
086 var rs, id, i, fs = [], vs = [], fs_str = vs_str =? sql = '';
087 for(i in values){
088 fs.push('[' + i + ']');
089 vs.push("'" + values[i] + "'");
090 }
091 fs_str = fs.join(",");
092 vs_str = vs.join(",");
093 sql = "Insert Into [" +table+ "](" +fs_str+ ") Values(" +vs_str+ ")";
094 rs = Server.CreateObject("ADODB.Recordset");
095 rs.ActiveConnection = this.connection;
096 rs.CursorType = 1;
097 rs.LockType = 2;
098 rs.Open("Select * From ["+table+"] Where 0");
099 rs.AddNew();
100 for(i in values){
101 //这里是判断字段类型,不是很准确
102 if(rs.fields(i).Type == 3){
103 rs(i) = int(values[i]);
104 }else{
105 rs(i) = values[i];
106 }
107 }
108 rs.Update();
109 id = rs(0) + '';
110 rs.Close();
111 return id;
112 }
113
114 //删除数据
115 ,del : function(table, wheres){
116 var sql;
117 sql = "Delete From [" +table+ "] Where " + wheres.join(" And ");
118 return this.execute(sql);
119 }
120
121 //向表插入数据
122 ,update : function (table, values, wheres){
123 var rs, i, set_str=[], sql = '';
124 for(i in values){
125 set_str.push('[' + i + ']' + "='" + values[i] + "'");
126 }
127 set_str = set_str.join(", ");
128 sql = "Update [" +table+ "] Set " +set_str+ " Where " + wheres.join(" And ");
129 this.execute(sql);
130 return true;
131 }
132 }
133
134 %>