CJJ专用ASP类库中的某个class |
作为程序员,相信每个人都有自己的函数库及类库 。在做项目时,从库里面提取想要的函数及类 。这样可以提高开发效率 。CJJ专用ASP类库中的某个class 复制代码 代码如下: ----******************** TConnString *****************************---- 数据库连接字符串结构体 Class TConnString Public DBName,DBPath,DBServer,DBUser,DBPass,DBType End Class ----******************** TConnString *****************************---- ----********************* TDBOperate *****************************---- 通用数据库操作类 Class TDBOperate Private cls_oConn,cls_oRS 类私有Connection对象、RecordSet对象 Private cls_sErrInfo,cls_sConn,cls_sSQL,cls_sURL,cls_sFormAction Private cls_iPageSize 分页数 Private cls_lTotalPage,cls_lTotalRecord,cls_lPageNo 类初始化 Private Sub Class_Initialize() End Sub ***************************************** 类型: 属性 目的: 根据获取的Connection String,创建数据库连接 输入: a_sConn:数据类型字符串 返回: 无 ***************************************** Public Property Let SetConn(a_sConn) Dim sObjType sObjType = LCase(TypeName(a_sConn)) If sObjType <> "string" Then cls_sErrInfo = cls_sErrInfo & "<li>SetConn:非法的字符串参数</li>" & Chr(10) Exit Property End If Set cls_oConn = CreateObject("Adodb.Connection") On Error Resume Next cls_oConn.Open a_sConn If Err Then Err.Clear Set cls_oConn = Nothing On error goto 0 cls_sErrInfo = cls_sErrInfo & "<li>数据库连接出错</li>" & Chr(10) End If On Error Goto 0 End Property ***************************************** 类型: 属性 目的: 根据获取的Connection对象,创建数据库连接 输入: a_oConn:数据类型字符串 返回: 无 ***************************************** Public Property Set SetConn(a_oConn) Dim sObjType,sConn Dim oConnStr sObjType = LCase(TypeName(a_oConn)) Select Case sObjType Case "connection" 设置Connection对象 Set cls_oConn = a_oConn Case "tconnstring" sConn = "" Set oConnStr = a_oConn Select Case (oConnStr.DBType) Case gbl_iDB_Access sConn = "Provider = micorsoft.jet.oledb.4.0; User ID = " & oConnStr.DBUser & "; Password = " & Replace(oConnStr.DBPass, Chr(0), "") & ";Initial Catalog = " & oConnStr.DBName & "; Data Source = " & SqlLocalName & ";" Case gbl_iDB_MsSQL sConn = "Provider = Sqloledb; User ID = " & oConnStr.DBUser & "; Password = " & Replace(oConnStr.DBPass, Chr(0), "") & ";Initial Catalog = " & oConnStr.DBName & "; Data Source = " & oConnStr.DBServer & ";" End Select If sConn = "" Then cls_sErrInfo = cls_sErrInfo & "<li>数据库连接对象出错,无法创建Connection对象</li>" & Chr(10) Exit Property End If 设置Connection连接串值,供ConnStr属性返回 cls_sConn = sConn Set cls_oConn = CreateObject("Adodb.Connection") On Error Resume Next cls_oConn.Open sConn If Err Then Err.Clear Set cls_oConn = Nothing cls_sErrInfo = cls_sErrInfo & "<li>数据库连接出错</li>" & Chr(10) End If On Error Goto 0 Case Else cls_sErrInfo = cls_sErrInfo & "<li>SetConn:非法的对象参数</li>" & Chr(10) Exit Property End Select End Property ***************************************** 类型: 属性 目的: 设置RecordSet对象 输入: a_sSQL: SQL语句 。 返回: 无 。 ***************************************** Public Property Let SetRS(a_sSQL) If LCase(TypeName(cls_oConn)) <> "connection" Then cls_sErrInfo = cls_sErrInfo & "<li>非法的Connection对象,无法创建RecordSet对象</li>" & Chr(10) Exit Property End If cls_sSQL = a_sSQL 创建RecordSet对象 Set cls_oRS = CreateObject("Adodb.RecordSet") On Error Resume Next cls_oRS.Open cls_sSQL,cls_oConn,1,1 On Error Goto 0 End Property ***************************************** 类型: 属性 目的: 设置RecordSet对象 输入: a_oRS: RecordSet对象 返回: 无 。 ***************************************** Public Property Set SetRS(a_oRS) If LCase(TypeName(a_oRS))<>"recordset" Then cls_sErrInfo = cls_sErrInfo & "<li>非法的RecordSet对象</li>" & Chr(10) Exit Property End If 设置RecordSet对象 Set cls_oRS = a_oRS End Property ***************************************** 类型: 属性 目的: 设置RecordSet对象 输入: a_oRS: RecordSet对象 返回: 返回一RecordSet对象 ***************************************** Public Property Get GetRS() Set GetRS = cls_oRS End Property 获取Connection对象 Public Property Get GetConn() If cls_sErrInfo <> "" Then Call ShowError() End If If LCase(TypeName(cls_oConn))<>"connection" Then cls_sErrInfo = cls_sErrInfo & "<li>Connection对象获取失败</li>" Exit Property End If Set GetConn = cls_oConn End Property 返回数据库连接字符串 Public Property Get ConnStr ConnStr = cls_sConn End Property 设置第个页面显示的数据数 Public Property Let PageSize(a_iPageSize) If Not IsNumeric(a_iPageSize) Then cls_sErrInfo = cls_sErrInfo & "<li>无效的分页记录数参数</li>" & Chr(10) Exit Property End If cls_iPageSize = a_iPageSize End Property 设置SQL语句,用于建立RecordSet对象 Public Property Let SQL(a_sSQL) If IsNone(a_sSQL) Then cls_sErrInfo = cls_sErrInfo & "<li>没有设置SQL,无法创建RecordSet对象</li>" & Chr(10) Exit Property End If cls_sSQL = Trim(a_sSQL) End Property 执行数据操作 Public Sub Execute() If cls_sErrInfo <> "" Then ShowError("<ul>" & Chr(10) & cls_sErrInfo & "</ul>" & Chr(10)) Exit Sub End If If LCase(TypeName(cls_oConn))="connection" Then If IsNumeric(cls_iPageSize) Then Set cls_oRS = CreateObject("Adodb.RecordSet") cls_oRS.Open cls_sSQL,cls_oConn,1,1 Else End If Else cls_sErrInfo = cls_sErrInfo & "<li>非法的Connection对象</li>" & Chr(10) End If End Sub ***************************************** 类型: 属性 目的: 设定或显示URL 。 输入: a_sURL: 需要分页的文件地址 。 返回: 无 ***************************************** Public Property Let URL(ByVal a_sURL) cls_sURL = a_sURL End Property ***************************************** 类型: 过程 目的: 统计总记录数、计算总页数 输入: 无 返回: 无 ***************************************** Private Sub Pagination(ByVal a_sStr) Dim iPosition,cls_sErrInfo,i,oRS_Temp,lTotalRecord If cls_sErrInfo <> "" Then Call ShowErrors() Exit Sub End If If cls_oRS.Eof And cls_oRS.Bof Then cls_sErrInfo = cls_sErrInfo & "<li>库中无任何记录</li>" End If 计算总计录数 Select Case LCase(TypeName(a_sStr)) Case "string" Set oRS_Temp = cls_oConn.Execute(a_sStr) lTotalRecord = CLng(oRS_Temp(0).Value) Case "integer" Select Case (Int(Trim(a_sStr))) Case gbl_iPagination_UseRcdCount 使的RecordCount方法进行分页 lTotalRecord = cls_oRS.RecordCount Case gbl_iPagination_UsePgCount 使用PageCount方法进行分页 lTotalRecord = cls_oRS.PageCount * cls_iPageSize End Select End Select cls_lTotalRecord = lTotalRecord If (cls_lTotalRecord<=2147483647 AND cls_lTotalRecord>=-2147483648) Then cls_lTotalRecord = CLng(cls_lTotalRecord) Else cls_lTotalRecord = 2147483647 End If If cls_lTotalRecord <0 Then cls_lTotalRecord = 0 End If 计算总页数 If cls_lTotalRecord Mod cls_iPageSize = 0 Then cls_lTotalPage = CLng(cls_lTotalRecord \ cls_iPageSize * -1)*-1 Else cls_lTotalPage = CLng(cls_lTotalRecord \ cls_iPageSize * -1)*-1 + 1 End If 获取当前页参数 cls_lPageNo = Trim(Request.QueryString("Page")) If cls_lPageNo = "" Then cls_lPageNo = Trim(Request.Form("Page")) If cls_lPageNo = "" Then cls_lPageNo = 1 End If End If 如果没有选择第几页,则默认显示第一页 If cls_lPageNo <> "" And IsNumeric(cls_lPageNo) Then If (cls_lPageNo <= 2147483647 And cls_lPageNo>=-2147483648) Then cls_lPageNo = CLng(cls_lPageNo) Else cls_lPageNo = 2147483647 End If If (cls_lPageNo<=0) Then cls_lPageNo = 1 End If Else 当前页参数为空或者非数字,默认将转到第1页 cls_lPageNo=1 End If If (cls_lPageNo > cls_lTotalPage AND cls_lTotalPage<>0) Then cls_lPageNo = cls_lTotalPage End If cls_oRS.PageSize = cls_iPageSize cls_oRS.AbsolutePage = cls_lPageNo iPosition = InstrRev(cls_sURL,"?") cls_sFormAction = cls_sURL If iPosition > 0 Then cls_sURL = cls_sURL & "&Page=" Else cls_sURL = cls_sURL & "?Page=" End If End Sub ***************************************** 类型: 过程 目的: 显示分页信息 输入: 无 返回: 无 ***************************************** Public Sub Pages(ByVal a_sStr) Dim strPages,k,intTemp,intTemp1 Dim sResult If Not IsNone(cls_sErrInfo) Then Call ShowErrors() End If 计算总页数及总记录数 Call Pagination(a_sStr) If cls_lTotalPage = 1 Then Exit Sub sResult = sResult & "<table class=""clsShowPage"">" & Chr(10) sResult = sResult & " <tr>" & Chr(10) & " <td>" & Chr(10) sResult = sResult & " <table width=""100%"">" & Chr(10) sResult = sResult & " <tr>" & Chr(10) & " <td class=""PageText"">" & Chr(10) If cls_lTotalPage >= 1 Then If cls_lPageNo <= 1 Then sResult = sResult & "首页 前页 <a href=""" & cls_sURL & cls_lPageNo+1 & """>后页</a> <a href=""" & cls_sURL & cls_lTotalPage & """>末页</a>" & Chr(10) Else If cls_lPageNo >= cls_lTotalPage Then sResult = sResult & "<a href=""" & cls_sURL & "1"">首页</a> <a href=""" & cls_sURL & cls_lPageNo -1 & """>前页</a> " & "后页 末页" & Chr(10) Else sResult = sResult & "<a href=""" & cls_sURL & "1"">首页</a> <a href=""" & cls_sURL & cls_lPageNo -1 & """>前页</a> " & "<a href=""" & cls_sURL & cls_lPageNo+1 & """>后页</a> <a href=""" & cls_sURL & cls_lTotalPage & """>末页</a>" & Chr(10) End If End If sResult = sResult & " 页次:<strong>" & cls_lPageNo & "</strong>/" & cls_lTotalPage & "页 共<strong>" & cls_lTotalRecord & "</strong>条记录 <strong>" & cls_iPageSize & "</strong>条/页</td>" & Chr(10) sResult = sResult & " <form name=""gopage"" action=""" & cls_sFormAction & """ method=""post"">" & Chr(10) sResult = sResult & " <td> 第" sResult = sResult & " <input type=""text"" name=""pageno"" class=""InputPage"" title=""请输入页号,然后回车"">页 " & Chr(10) sResult = sResult & "<input type=""submit"" class=""GotoPage"" value=""GO""></td></form></tr>" & Chr(10) End If sResult = sResult & " </table>" & Chr(10) & " </td>" & Chr(10) & " </tr>" & Chr(10) & "</table>" & Chr(10) 输出分页信息 Response.Write("result:" & sResult) End Sub 类销毁 Private Sub Class_Terminate() If LCase(TypeName(cls_oConn))<>"nothing" Then cls_oConn.Close Set cls_oConn = Nothing End If If LCase(TypeName(cls_oRS))<>"nothing" Then cls_oRS.Close Set cls_oRS = Nothing End If End Sub ***************************************** 类型: 过程 目的: 显示分页类中出现的错误信息 输入: 无 返回: 无 ***************************************** Private Sub ShowErrors() If cls_cls_sErrInfo <> "" Then cls_cls_sErrInfo = "<ul>" & Chr(10) & cls_sErrInfo & "</ul>" & Chr(10) Response.Write(cls_cls_sErrInfo) Response.End End If End Sub End Class ----********************* TDBOperate *****************************---- 稍提一个编码风格 个人觉得代码混排是个鸡肋,混排的可读性差,所以我一般都只是少量混排,尽量将代码和HTML分离 。 复制代码 代码如下: <!--#include file="pubdb.asp"--> sErrInfo = "" Dim oRS,sHtml Call OpenDB() 循环读取数据库中的表名 <html> function updateData() { var aElmnts = sDstID.split(,); if ((oXml.readyState == 4) && (oXml.status == 200)) { var oXml = false; <div id="fieldcount">表字段个数:<input type="text" id="txtFldCount" name="txtFldCount" value="0" /></div> |