只看楼主 楼主

asp类的操作

这是网上一个牛人写的,请给个实例吧??

<%
//基本操作类用Javascript实现的一个数据库操作类
Response.Expires=-1000;
Response.CacheControl="no-cache";

function DBOperate()
{      
   this.connString=""; //连接字符串
   this.connString="Provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath("/sms/dbo/aaa@163.com/#data.asp");
   //this.connString="driver={SQL SERVER};server=localhost;database=user;uid=study;pwd=study;";
   this.conn; //数据库连接对象
   this.rs;   //数据集  
   this.cursorType=1; //纪录集移动方式:
   this.lockType=1; //纪录集锁定方式:
   //*************************************************************************
   //当分页的时候,计算totalRecordCount和totalPageCount
   //*************************************************************************
   this.totalRecordCount=0;  //纪录集总纪录数,开始由于没纪录集,所以为0
   this.pageSize=10;    //每页最大纪录数,默认为10
   this.totalPageCount=0;  //最大页数.  
   //*************************************************************************
   this.currentPageID=1;  //当前页码,默认为1
   this.currentPageTag="CurrentPageID";
   this.gotoPageName="?";  //显示纪录的页面名称,默认为当前页面
   this.recordIndexInPage=0;  //用于分页时的数据下移
 
   //打开数据库
   this.openDatabase=function(_dbPath)
   {
       if (_dbPath!=undefined)
       {
           this.connString="Provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath(_dbPath);
       }
       try
       {
           this.conn=Server.CreateObject("ADODB.Connection");
           this.conn.Open(this.connString);
       }
       catch (e)
       {
           Response.Write("数据库连接错误!");
           Response.End();
       }          
   }
 
   //关闭数据库
   this.closeDatabase=function()
   {
       if (this.rs!=null)
       {
           this.rs.Close();
           this.rs=null;
       }
       if (this.conn!=null)
       {
           this.conn.Close();
           this.conn=null;
       }
   }
 
   //获取一个数据集对象
   this.getRecordSet=function(_sqlString)
   {
       if (this.conn!=null)
       {
           var i=Request.QueryString(this.currentPageTag).Count;
           if (i>0)
           {
               this.currentPageID=parseInt(Request.QueryString(this.currentPageTag));
               if (this.currentPageID<=0)
               {
                   this.closeDatabase();
                   Response.Write("页码超出合法范围!");
                   Response.End();
               }
           }
           this.rs=Server.CreateObject("ADODB.RecordSet");  
           this.rs.Open(_sqlString,this.conn,this.cursorType,this.lockType);              
           this.totalRecordCount=this.rs.RecordCount;
           this.totalPageCount=Math.ceil(this.totalRecordCount/this.pageSize);
           var endPage=this.totalPageCount;
           if (endPage==0) endPage=1;
           if (this.currentPageID>endPage)
           {
               this.closeDatabase();
               Response.Write("页码超出合法范围!"+this.pageSize);
               Response.End();
           }
           //指针位置调整
           if (!this.rsIsEmpty() && this.currentPageID>1)
           {
               this.rs.MoveFirst();                  
               this.rs.Move((this.currentPageID-1)*this.pageSize,1);
           }              
       }              
       else
       {
           Response.Write("没有连接到数据库!");
           Response.End();
       }
   }
 
   //判断分页的时候记录是否已到一页的末尾
   this.isPageEnd=function()
   {
       this.rs.moveNext();
       this.recordIndexInPage++;
       if (this.recordIndexInPage<this.pageSize)
       {              
           return false;
       }
       else
       {
           return true;
       }
   }
 
   //关闭数据集对象
   this.closeRecordSet=function()
   {
       if (this.rs!=null)
       {
           this.rs.Close();
           this.rs=null;
       }
   }
 
   //执行SQL语句,用于执行添加、删除、修改操作
   this.executeSql=function(_sqlString)
   {
       if (this.conn!=null)
       {
           this.conn.Execute(_sqlString);
       }
       else
       {
           Response.Write("没有连接到数据库!");
           Response.End();
       }
   }
 
   this.addNewAndReturnPK=function(_parameterMap,_tableName,_pkName)
   {      
       var returnValue="";
       if (this.rs==null)
       {
           this.rs=Server.CreateObject("ADODB.RecordSet");
           this.rs.ActiveConnection=this.conn;
           this.rs.CursorType=this.cursorType;
       }
       this.rs.LockType=3;
       this.rs.Source=_tableName;
       this.rs.Open();
       this.rs.AddNew();
       var keys=_parameterMap.keys();      
       for (var i=0;i<keys.length;i++)
       {
           //Response.Write(keys[i]+":");
           //Response.Write(_parameterMap.get(keys[i])+"<br>");
           try
           {
               this.rs(keys[i])=_parameterMap.get(keys[i]);
           }
           catch (e)
           {}
       }
       this.rs.Update();
       returnValue=this.rs(_pkName).value;
       this.rs.Close();
       this.rs.LockType=this.lockType;  
       this.rs.Open();
       return returnValue;
   }
 
   this.updateRecord=function(_parameterMap,_tableName,_pkValue,_pkName)
   {
       if (this.rs==null)
       {
           this.rs=Server.CreateObject("ADODB.RecordSet");
           this.rs.ActiveConnection=this.conn;
           this.rs.CursorType=this.cursorType;
       }
       this.rs.LockType=3;
       this.rs.Source=_tableName;
       this.rs.Open();
       this.rs.Find(_pkName+"="+_pkValue);
       var keys=_parameterMap.keys();
       for (var i=0;i<keys.length;i++)
       {
           try
           {
               this.rs(keys[i])=_parameterMap.get(keys[i]);
           }
           catch (e)
           {}          
       }
       this.rs.Update();
       this.rs.Close();
       this.rs.LockType=this.lockType;  
       this.rs.Open();
   }
 
   //判断获取的数据集对象是否为空
   this.rsIsEmpty=function()
   {
       if (this.rs!=null)
       {
           if ((this.rs.BOF) && (this.rs.EOF))
           {
               //RS is empty
               return true;
           }
           else
           {
               //RS not empty
               return false;
           }
       }
       else
       {
           Response.Write("没有连接到数据库!");
           Response.End();
       }          
   }
}
%>
CHINAZ官方广告

 TOP

只看该用户 沙发

Re:asp类的操作

用JavaScript写地,还是真是牛人耶。

 TOP

只看该用户 板凳

Re:asp类的操作

哪里是javascript?
azige的签名

 TOP

只看该用户 地板

Re:asp类的操作

<%
%>

这样的是JS?

 TOP

只看该用户 #4

Re:asp类的操作

晕。乱来!
7421021的签名
支持奥运,支持中国音乐www.gdmix.com

 TOP

只看该用户 #5

Re:asp类的操作

引用:
以下是会员74210212008-8-17 14:19:24发表的内容
晕。乱来!


IIS 支持VBSCRIPT和JAVASCRIPT服务器端脚本。
dreamcoco的签名
唐伯虎点秋香;艾哎斯批点奈特!





 TOP

只看该用户 #6

Re:asp类的操作

说的对







恒温培养箱|低温恒温槽|恒温摇床|恒温恒湿培养箱

 TOP

只看该用户 #7

Re:asp类的操作

实验证明,用Jscript写出的ASP执行效率比VBScript要高好多!
柯南~~的签名
只玩小游戏,永不玩网游...

 TOP

只看该用户 #8

Re:asp类的操作

asp用类效率不一定高.
xinbi的签名

 TOP

只看该用户 #9

Re:asp类的操作

效率肯定低的。
wxj_16740的签名

 TOP

只看该用户 #10

Re:asp类的操作

打着JS的牌子,用ASP语言垫底!
实在不厚道,有失程序员的朴实品质!!!

 TOP

只看该用户 #11

Re:asp类的操作

楼主 这不是类,只是很普通的调用函数,而且执行效率特不高,可以说是 根本不符合数据库 操作
happyhm的签名
㊣━━^-^o科域酷网专用印o^-^━━㊣
┃ 有事联系 QQ 9143844 2007-2008┃
专 本人常缺 ASP JSP PHP 测试空间 印
用 本人常缺 RMB 美元 希望大家提供 章
┃ 本人网站 http://www.xhmsn.cn ┃
㊣━━^-^o科域酷网专用印o^-^━━㊣

 TOP