在线用户    站点地图 技术论坛 文档中心 站点导航 资源下载 语言参考 教程专题 站点统计 业务项目 使用帮助
您的位置
MSDN参考新加了CSS2参考,全中文,制作精良。245k下载。           新加了sxf_sxf推荐的"动态网页专家指导" 下载!          新加了lsg2002lsg推荐的"ASP.NET高级编程(PDF)" 下载!          新加了DHTML代码预览!!,功能强大哦!欢迎试用          新加了网友lincong为大家提供的SQL Server 2000 程序员指南,有点大,但同时也就说明内容全咯!           新加了网友ibra为大家提供的北大的JAVA课件,很适合初学者入门           新加了一个java的连接缓冲池代码示例           新加了人月神话电子版下载!软件工程巨著哦~~           更新了评分标准,引入了“专家分”概念,相信可以更好的激励大家的学习!详情请看“使用帮助”           新加了由网友GGG提供的“IIS超级管理手册”,值得一看哦!(虽然我没看过 :P )           新加了“英雄榜”,欢迎测试!           “无组件上传·从原理剖析到实践”已经全部完成,源码全部开放,有兴趣的朋友请到文档中心一探究竟。  您的位置  首页>文档中心>ASP>文档A0000926 在同一窗口中打开页面中的链接 在新窗口中打开页面中的链接 将ASPCHINA设为首页 将ASPCHINA加入书签    
 LOGIN HERE
用户:
密码: 忘记密码
记住密码 用户注册 游客进入
相关文章

一个智能化的日期时间类CDateTimeEdit

子类化功能的理解和使用

站长兄弟,你写的一个类有错哦!

StringBuilder类

SQL SERVER的数据类型

ASP编写完整的一个IP所在地搜索类

Vbscript类型转换函数

有关时间类函数

enum类型的重载操作

领会名字查找规则
发表文章
[分享]仿PHP操作数据库的类

作者:sandy_jay     提交人:sandy_jay     发布时间:06-01-13     文章类型:原创     浏览量:3796
参考链结:http://    
关键字:类|数据库操作
[本文档没有附件]
字体:    视力保护色: 杏仁黄  秋叶褐  胭脂红  芥末绿  天蓝  雪青  灰  银河白(默认色) [A0000926]

原先在php下写了个操作数据库的类,我用jscript重写了一遍。有兴趣大家可以试用一下,有什么错误希望大家指出。:-)
<%@LANGUAGE="JScript" CODEPAGE="65001"%>
<%
// Sample
var db = new dbstuff();
db.connect(dbserver, dbuser, dbpwd, dbname);

var sql = "SELECT @@VERSION as dbv";
var query = db.query(sql);
while(arr = db.fetch_array(query)){
  echo(arr["dbv"]);
}
db.close();
///////////////////

function dbstuff(){
  ///////////////////
  // Attributes
  ///////////////////
  // Private
  var conn = Server.CreateObject("ADODB.Connection");
  // Public
  this.state = false;
  this.querynum = 0;
  ///////////////////
  // Methods
  ///////////////////
  // Open a new connection to the MSSQL server
  this.connect = function(dbserver, dbuser, dbpwd, dbname){
    try{
      if(this.state){
        conn.Close();
        this.state = false;
      }
      conn.CommandTimeout = 180;
      conn.ConnectionString = "Provider=SQLOLEDB; Data Source="+dbserver+"; UID="+dbuser+"; PWD="+dbpwd+";Initial Catalog="+dbname;
      conn.Open();
    }catch(e){
      die("Can not connect to Database Server.");
    }
    this.state = true;
  }

  // Performs a query on the database
  this.query = function(sql){
    try{
      var query = conn.Execute(sql);
    }catch(e){
      die("Database Query Error.", e);
    }
    this.querynum++;
    return query;
  }

  // Fetch a result row as an associative array
  this.fetch_array = function(rs, freeresult){
    if(rs == undefined){return false;}
    try{
      if(rs.State != 1){return false;}
    }catch(e){
      die("Fetch Data Error.", e);
    }
    if(!rs.EOF){
      var arr = new Array();
      var colums = rs.Fields.Count;
      var i, fld;
      for(i=0; i < colums; i++){
        arr[rs.Fields(i).Name.toLowerCase()] = rs.Fields(i).Value;
      }
      rs.MoveNext();
      return arr;
    }else{
      if(freeresult == undefined){
        this.free_result(rs);
      }
      return false;
    }
  }

  // Fetch a result row as enumerated array
  this.fetch_row = function(rs, freeresult){
    if(rs == undefined){return false;}
    try{
      if(rs.State != 1){return false;}
    }catch(e){
      die("Fetch Data Error.", e);
    }
    if(!rs.EOF){
      var arr = new Array();
      var colums = rs.Fields.Count;
      for(var i=0; i < colums; i++){
        arr[i] = rs.Fields(i).Value;
      }
      rs.MoveNext();
      return arr;
    }else{
      if(freeresult == undefined){
        this.free_result(rs);
      }
      return false;
    }
  }

  // Fetch all result rows as associative array
  this.fetch_all = function(rs, freeresult) {
    if(rs == undefined){return false;}
    try{
      if(rs.State != 1){return false;}
    }catch(e){
      die("Fetch Data Error.", e);
    }
    var arr = new Array();
    var colums = rs.Fields.Count;
    var rows = 0;
    while(!rs.EOF){
      arr[rows] = new Array();
      for(i=0; i < colums; i++){
        arr[rows][rs.Fields(i).Name.toLowerCase()] = rs.Fields(i).Value;
      }
      rows++;
      rs.MoveNext();
    }
    if(freeresult == undefined){
      this.free_result(rs);
    }
    return arr;
  }

  // Open recordset
  this.recordset = function(sql, cursortype, locktype){
    if(cursortype == undefined){cursortype = 3;}
    if(locktype == undefined){locktype = 3;}
    try{
      var rs = Server.CreateObject("ADODB.RecordSet");
      rs.CursorLocation = 3;
      rs.Open(sql, conn, cursortype, locktype);
    }catch(e){
      die("Database Query Error.", e);
    }
    this.querynum++;
    return rs;
  }

  // Fetch all result rows in one page as associative array
  this.fetch_page = function(sql, pagesize, curpage, cursortype, locktype){
    if(pagesize == undefined){pagesize = 20;}
    if(curpage == undefined){curpage = 1;}
    if(cursortype == undefined){cursortype = 3;}
    if(locktype == undefined){locktype = 3;}
    try{
      var rs = Server.CreateObject("ADODB.RecordSet");
      rs.CursorLocation = 3;
      rs.CacheSize = pagesize;
      rs.Open(sql, conn, cursortype, locktype);
    }catch(e){
      die("Database Query Error.", e);
    }
    this.querynum++;

    if(rs.State != 1){
      return false;
    } else {
      this.recordcount = rs.recordcount;
      rs.PageSize = pagesize;
      if(!curpage){
        curpage = 1;
      }else if(curpage > rs.PageCount){
        curpage = rs.PageCount;
      }
      rs.AbsolutePage = curpage;
      var arr = new Array();
      var colums = rs.Fields.Count;
      for(i=0; i< pagesize; i++){
        arr[i] = new Array();
        for(j=0; j < colums; j++){
          arr[i][rs.Fields(j).Name.toLowerCase()] = rs.Fields(j).Value;
        }
        rs.MoveNext();
        if(rs.EOF){break;}
      }
      this.free_result(rs);
      return arr;
    }
  }

  // Fetch one field value of a result row
  this.result = function(rs, row, mixedfld, freeresult){
    if(rs == undefined) {return false;}
    try{
      if(rs.State != 1){return false;}
    }catch(e){
      die("Fetch Data Error.", e);
    }
    if(row == undefined) {row = 0;}
    if(mixedfld == undefined) {mixedfld = 0;}
    if(free == undefined) {
      free = true;
    }else{
      free = false;
    }
    if(rs.EOF){
      this.free_result(rs);
      return false;
    }
    if(row){rs.Move(row);}
    var returnvalue = rs.Fields[mixedfld].value;
    if(freeresult){this.free_result(rs);}
    return returnvalue;
  }

  // Get the number of rows in a result
  this.num_rows = function(rs){
    if(rs == undefined) {return false;}
    try{
      if(rs.State != 1){return false;}
    }catch(e){
      die("Fetch Data Error.", e);
    }
    return rs.RecordCount();
  }

  // Get the number of fields in a result
  this.num_fields = function(rs){
    if(rs == undefined) {return false;}
    try{
      if(rs.State != 1){return false;}
    }catch(e){
      die("Fetch Data Error.", e);
    }
    return rs.Fields.Count;
  }
 
  // Get the auto generated id used in the last query
  this.insert_id = function(){
    query = this.query("SELECT @@IDENTITY");
    return this.result(query);
  }

  // Get the number of affected rows in a previous SQL operation
  // It should be called immediately after a SQL operation
  this.affected_rows = function(){
    query = this.query("SELECT @@ROWCOUNT");
    return this.result(query);
  }
  // Get the mssql version
  this.version = function(){
    query = this.query("SELECT @@VERSION");
    return this.result(query);
  }
  // Release resource
  this.free_result = function(rs){
    if(rs == undefined){return;}
    try{
      if(rs.State == 1){
        rs.Close();
      }
      rs = null;
    }catch(e){
      die("Close RecordSet Error.", e);
    }
  }
  // Close db connection
  this.close = function(){
    if(this.state){
      try{
        conn.Close();
        conn = null;
      }catch(e){
        die("Close Database Connection Error.", e);
      }
      this.state = false;
    }
  }
}

///////////////////

function echo(str){
  Response.Write(str+"\n");
}

function nocache(){
  Response.AddHeader("pragma","no-cache");
  Response.AddHeader("cache-control","private");
  Response.CacheControl = "no-cache";
}

function die(errmsg, err){
  Response.Clear();
  nocache();
  Response.ContentType = "text/vnd.wap.wml;";
  Response.CharSet = "utf-8";
  // error message
  if(errmsg == undefined){errmsg="";}
  echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!DOCTYPE wml PUBLIC \"-//OPENWAVE.COM//DTD WML 1.3//EN\" \"http://www.openwave.com/dtd/wml13.dtd\">\n<wml>\n<card>");
  if(errmsg) echo("<p>"+errmsg+"</p>");
  // error detail
  if(err != undefined){
    if(err.constructor == Error){
      echo("<p>Error Number: " + String(err.number & 0xFFFF) + "</p>\n<p>Error Detail: "+err.description+"</p>");
    }
  }
  echo("</card>\n</wml>");
  Response.End();
}
%>

关于这篇文章的评论 [注意:这里仅仅是给大家提供了一个发表对文章本身看法的地方,如果有疑问,请到论坛提出] 我要提问!
qweqwe        2009/12/29 11:13:00
:)
You can obtain Resveratrol via supplements and wow power leveling standardised doses and they are wow power leveling essential if you want wow gold uk to make sure that you get enough to give your body a helping hand in cheapest wow gold the fight against disease and aging. This form gold for wow of it is actually taken from the roots of plants so it is undiluted and guaranteed to work.



miles        2006/2/15 20:03:00
:)
不错不错 !!!
标题
内容
发言
*您尚未以注册用户身份登录,不能发表评论。这里登录
您的位置
  (c)2000-2019 Yup Studio, all rights reserved.  
337.8906