作為程序員,相信每個人都有自己的函數庫及類庫。在做項目時,從庫裡面提取想要的函數及類。這樣可以提高開發效率。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"-->
<%
'*****************************************
'類型:函數
'目的:報錯
'參數:
'a_num:報錯信息參數
'*****************************************
Private Function ShowError(a_Num)
Dim sErrInfo
sErrInfo = ""
Response.Write("<p>Error Number:era_" & a_Num & "</p>")
Select Case a_Num
Case "1000"
sErrInfo = "參數類型不正確,請檢查"
Case "1100"
sErrInfo = "無法打開數據庫連接"
Case Else
sErrInfo = "發現未知錯誤,請與管理員聯系"
End Select
sErrInfo = "<p>Error Description:" & sErrInfo & "</p>"
Response.Write(sErrInfo)
Response.End
End Function
Dim oRS,sHtml
Call OpenDB()
Set oRS = oConn.OpenSchema(20)
sHtml=""
oRS.MoveFirst
'循環讀取數據庫中的表名
Do While Not oRS.EOF
If UCase(oRS(3))="TABLE" Then
sTemp = Trim(oRS(2))
If sTBName = sTemp Then
sHtml= sHtml & Space(2) & "<option value=""" & sTemp & """ selected=""selected"">" & sTemp & "</option>" & Chr(10)
Else
sHtml= sHtml & Space(2) & "<option value=""" & sTemp & """>" & sTemp & "</option>" & Chr(10)
End If
End If
oRS.MoveNext
Loop
Call CloseDB()
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>數據庫名</title>
<script type="text/javascript">
<!--
var sDstID="";
function serverResult(a_sUrl,a_sSrcID,a_sDstID) {
var sValue = document.getElementById(a_sSrcID).value;
sDstID=a_sDstID;
if ((sValue == null) || (sValue == "")) return;
var sUrl = a_sUrl+sValue;
oXml.open("GET", sUrl,true);
oXml.onreadystatechange = updateData;
oXml.send(null);
}
function updateData() {
var aElmnts = sDstID.split(',');
var oElmnt = null;
var aTags=null;
var oXmlData=null;
if ((oXml.readyState == 4) && (oXml.status == 200)) {
aTags = oXml.responseXML.documentElement.getElementsByTagName("cjjitem");
if (aTags.length!=aElmnts.length) {
alert('獲取的服務器端的數據錯誤!');
return null;
}
for (var i=0;i<aElmnts.length ;i++ ) {
oElmnt = document.getElementById(aElmnts[i]);
oElmnt.innerHTML=aTags[i].firstChild.data;
}
}
return true;
}
var oXml = false;
if (window.ActiveXObject) {
oXml = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
oXml=new XMLHttpRequest();
}
//-->
</script>
</head>
<body>
<form method="POST" name="form1" action="addFormData.asp">
<p> </p>
<p>數據庫名:<input type="text" name="txtDBName" size="7" value="work"> 數據庫用戶名:<input type="text" name="DBUserName" size="8" value="sa">數據庫密碼:<input type="password" name="DBUserPassWord" size="10" value=""> 數據庫服務器路徑:<input type="text" name="DBServerPath" size="20" value="127.0.0.1"></p>
<p>數據表名:<select size="1" id="sltTBName" name="sltTBName" onchange="serverResult('getFieldList.asp?n=','sltTBName','fieldcount,tblFields');">
<option selected="selected">請選擇一個表</option>
<%=sHtml%>
</select></p>
<div id="fieldcount">表字段個數:<input type="text" id="txtFldCount" name="txtFldCount" value="0" /></div>
<table id="tblFields" border="1" width="91%">
<thead>
<tr>
<td align="center" width="94">字段名</td>
<td align="center" width="113">字段類型</td>
<td width="27" align="center">使用</td>
<td width="18" align="center">只讀</td>
<td align="center" width="80">表單項類型</td>
<td align="center" width="100">表單項名稱</td>
<td align="center" width="92">表單項描述</td>
<td align="center" width="87">表單項驗證</td>
<td align="center">表單項默認值</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
<p align="left">需要生成的動態ASP網頁類型:<select size="1" name="sltAspType">
<option value="0">數據添加</option>
<option value="1">數據編輯</option>
<option value="2">數據刪除</option>
<option value="3">數據管理</option>
<option value="4">數據列表</option>
</select> 文件名:<input type="text" name="txtFileName" size="17" value="">
文件類型:<select size="1" name="sltFileType">
<option value="ASP">ASP</option>
<option value="PHP">PHP</option>
<option value="JSP">JSP</option>
<option value="PERL">PERL</option>
<option value="VB.NET">VB.NET</option>
<option value="C#">C#</option>
</select>
<input type="submit" value="生成文件" name="action"></p>
</form>
</body>
</html>