database.asp

来自「ZD-BS是一个基于asp+access的个人blog系统 程序特点: 1.」· ASP 代码 · 共 202 行

ASP
202
字号
<script language="jscript" runat="server">

//============================================================
// Copyright 2006 VAL/ZYI. All Rights Reserved.
//============================================================

function DataClass(){
	
	//create the connection object/////
	var db=Server.CreateObject("ADODB.Connection");
	
	//the access variable/////
	this.accPath="";
	
	this.opened=false;
	
	//the queryInfo/////////////
	this.recordCount=0;
	this.pageCount=0;
	this.resultCount=0;
	this.queryCount=0;
	
	this.conn=function(){
		if(!this.opened){
			try{
				var strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+this.accPath;
				db.Open(strConn);
			}
			catch(e){db_errMsg(1);}
			this.opened=true;
		}
		return true;
	};

	this.close=function(){
		if(this.opened){
			try{
				db.Close();
				delete db;
			}
			catch(e){db_errMsg(2);}
			this.opened=false;
		}
		return true;
	};

	this.exec=function(strSql){
		this.conn();
		try{db.Execute(strSql);}
		catch(e){db_errMsg(3);}
		this.close();
		this.queryCount++;
		return true;
	};

	this.query=function(strSql,numPageSize,numAbsolutePage,bVBArray){
		var rs=Server.CreateObject("ADODB.Recordset");
		this.conn();
		try{rs.Open(strSql,db,1,1);}
		catch(e){db_errMsg(4);}
		if(rs.Eof||rs.Bof){
			this.resultCount=0;
			this.pageCount=0;
			this.recordCount=0;
			rs.Close;
			delete rs;
			return null;
		}
		this.recordCount=rs.RecordCount;
		if(!numPageSize||numPageSize<1) numPageSize=rs.RecordCount;
		rs.PageSize=numPageSize;
		if(!numAbsolutePage||numAbsolutePage<1) numAbsolutePage=1;
		if(numAbsolutePage>rs.PageCount) numAbsolutePage=rs.PageCount;
		rs.AbsolutePage=numAbsolutePage;
		this.pageCount=rs.PageCount;
		var result=new Array();
		result=rs.GetRows(rs.PageSize);
		this.resultCount=result.ubound(2)+1
		if(!bVBArray){
			var x=result.ubound(2);
			var y=result.ubound(1);
			var re=new Array();
			for(var i=0;i<=x;i++){
				re[i]=new Array();
				for(var j=0;j<=y;j++){
					re[i][rs.Fields(j).Name]=result.getItem(j,i);
				}
			}
			result=re;
		}
		rs.Close;
		delete rs;
		this.close();
		this.queryCount++;
		return result;
	};

	this.insert=function(arrValue,strTable){
		var str1="",str2="",strSql="INSERT INTO "+strTable;
		for(var n in arrValue){
		if(arrValue[n]!=undefined){
			switch(arrValue[n].constructor){
				case Number:
					str1+=arrValue[n]+",";
					break;
				case Boolean:
					str1+=arrValue[n]+",";
					break;
				case String:
					str1+="'"+arrValue[n]+"',";
					break;
				case Date:
					str1+="#"+dateStr(arrValue[n])+"#,";
					break;
			}
			str2+=n+",";
		}
		}
		str1=str1.slice(0,-1);
		str2=str2.slice(0,-1);
		strSql+=" ("+str2+") VALUES("+str1+")";
		this.exec(strSql);
		return true;
		
	};
	
	this.update=function(arrValue,strTable,strWhere){
		var str="",strSql="UPDATE "+strTable+" SET ";
		for(var n in arrValue){
		if(arrValue[n]!=undefined){
			switch(arrValue[n].constructor){
				case Number:
					str+=n+"="+arrValue[n]+",";
					break;
				case Boolean:
					str+=n+"="+arrValue[n]+",";
					break;
				case String:
					str+=n+"='"+arrValue[n]+"',";
					break;
				case Date:
					str+=n+"=#"+dateStr(arrValue[n])+"#,";
					break;
			}
		}
		}
		str=str.slice(0,-1);
		strSql+=str;
		if(strWhere) strSql+=" WHERE "+strWhere;
		this.exec(strSql);
		return true;
	};
	
	this.updateSql=function(strSet,strTable,strWhere){
		var str="update "+strTable+" set "+strSet;
		if(strWhere) str+=" where "+strWhere;
		this.exec(str);
		return true;
	};

	this.del=function(strTable,strWhere){
		var str="DELETE FROM "+strTable;
		if(strWhere) str+=" WHERE "+strWhere;
		this.exec(str);
		return true;
	};
	
	function dateStr(objDate){
		var str="";
		str+=objDate.getYear()+"-";
		str+=(objDate.getMonth()+1)+"-";
		str+=objDate.getDate()+" ";
		str+=objDate.getHours()+":";
		str+=objDate.getMinutes()+":";
		str+=objDate.getSeconds();
		return str;
	}
	
	function db_errMsg(intMode){
		var str="";
		switch(intMode){
			case 1:
			str+="Error! Database connection method: [ this.conn() ]";
			break;
			case 2:
			str+="Error! Database connection method: [ this.close() ]";
			break;
			case 3:
			str+="Error! Database connection method: [ this.exec() ]";
			break;
			case 4:
			str+="Error! Database connection method: [ this.query() ]";
			break;
		}
		Response.Write(str);
		Response.End();
	}

}

</script>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?