中国站长论坛

中国站长论坛 ›› 网络编程 ›› asp类的操作

页码: 1 2

asp类的操作

- zhgzzy 2008-08-14 11:46

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

<%
//基本操作类用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();
       }          
   }
}
%>

Re:asp类的操作

- james1987 2008-08-14 14:46

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

Re:asp类的操作

- azige 2008-08-15 08:43

哪里是javascript?

Re:asp类的操作

- BJMM 2008-08-16 21:39

<%
%>

这样的是JS?

Re:asp类的操作

- 7421021 2008-08-17 14:19

晕。乱来!

Re:asp类的操作

- dreamcoco 2008-08-17 17:24

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


IIS 支持VBSCRIPT和JAVASCRIPT服务器端脚本。

Re:asp类的操作

- yysyya 2008-09-10 11:29

说的对







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

Re:asp类的操作

- 柯南~~ 2008-09-10 13:50

实验证明,用Jscript写出的ASP执行效率比VBScript要高好多!

Re:asp类的操作

- xinbi 2008-09-11 10:56

asp用类效率不一定高.

Re:asp类的操作

- wxj_16740 2008-09-13 17:31

效率肯定低的。

Re:asp类的操作

- red520 2008-09-15 09:29

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

Re:asp类的操作

- happyhm 2008-09-17 05:05

楼主 这不是类,只是很普通的调用函数,而且执行效率特不高,可以说是 根本不符合数据库 操作

页码: 1 2