<%@ Language=VBScript %>
<%Option Explicit%>
<!-- #INCLUDE FILE = "../inc.asp" -->
<%
CheckAdminLogin()
Class ADO
'熟悉面向对象编程的朋友，是不是很亲切？
	Private m_conn, m_rst, m_connstr
	Private m_query
	Private m_CursorLocation, m_CursorType, m_LockType
		
	Public Sub SetConnString(sConn)
		m_connstr = sConn
	End Sub
	
	Public Sub InitializeConnection()
		If IsEmpty(m_conn) Then
			Set m_conn = Server.CreateObject("ADODB.Connection")
			m_conn.Open m_connstr
		End If
	End Sub
	
	Public Sub SetQueryString(sQuery)
		m_query = sQuery
	End Sub
	
	Public Function GetConn()
		Set GetConn = m_conn
	End Function
			
	Public Function GetRecordset()
		If IsEmpty(m_conn) Then
			err("ADO类初始化错误：没有建立数据连接。")
			Exit Function
		End If
		If IsEmpty(m_rst) Then
			Set m_rst = Server.CreateObject("ADODB.RecordSet")
			m_rst.CursorLocation = m_CursorLocation
			m_rst.CursorType = m_CursorType
			m_rst.LockType = m_LockType
			
			'Response.Write m_query
			'Response.End
			m_rst.Open m_query, m_conn, , , &H0001
		End If
		Set GetRecordset = m_rst
	End Function
	
	Private Sub Class_Initialize
		m_CursorLocation = 2
		m_CursorType = 1
		m_LockType = 1
	End Sub
	
	Public Sub SetADOVariables(iCursorLocation, iCursorType, iLockType)
	'这是一些RecordSet对象的参数，具体用法我就不多说了，很多书都可以查
		m_CursorLocation = iCursorLocation'adUseNone = 1, adUseServer = 2, adUseClient = 3, adUseClientBatch = 3
		m_CursorType = iCursorType'Const adOpenForwardOnly = 0, Const adOpenKeyset = 1, Const adOpenDynamic = 2, Const adOpenStatic = 3
		m_LockType = iLockType'Const adLockReadOnly = 1, Const adLockPessimistic = 2, Const adLockOptimistic = 3,Const adLockBatchOptimistic = 4
	End Sub
	
	'Class_Terminate是对象销毁时要执行的函数，我们用它来断开数据库连接，并销毁连接对象
	Private Sub Class_Terminate
		If Not IsEmpty(m_conn) Then
			m_conn.Close
			Set m_conn = Nothing
		End If
	End Sub
	
	Private Sub err(sErr)
		Response.Write sErr
	End Sub
End Class


Function DisplayTable(sConn, sTableName, sHidden, sDisplay, sQuery, sOrder, iTop, iPageSize)
	Dim sHead, sPrevious, sNext, sFoot, sGo
	
	'定义按钮名称
	sHead = "首页" : sPrevious = "前一页" : sNext = "后一页" : sFoot = "尾页" : sGo = "转到"
	
	'<table>的属性
	Dim t_width, t_height, t_border, t_bordercolor, t_cellspacing, t_cellpadding, t_bgcolor, t_background, t_align
	'<tr>的属性
	Dim tr_align, tr_bgcolor
	'<th>的属性
	Dim th_bgcolor, th_backgound
	'<td>的属性
	Dim td_align, td_width, td_bgcolor
	
	'可以定义表格的样式，未写全，读者可以自己添加
	t_width = "100%"
	t_align = "center"
	t_cellspacing = 1
	t_cellpadding = 3
	t_border = 0
	t_bgcolor = ""
	th_bgcolor = "#DEE5FA"
	th_backgound = "images/admin_bg_1.gif"
	td_bgcolor = "#DEE5FA"
	td_width = ""
	'结束

	Dim i, oADO, aDisplay, aQuery, aHidden
	
	Dim sDispSQL
	Dim Key, sQuerySQL, sQueryString, iCount
	Response.Write "<form method=""get"" name=""form"">"
	
	'ConvertStr函数将传递的字符串参数分隔成数组
	'而数组的每个变量是一个Twins类，Twins是双胞胎的意思，我开始起名的时候只考虑到两个变量，随手起名Twins，可实际上是三兄弟哦。
	aDisplay = ConvertStr(sDisplay)
	aQuery = ConvertStr(sQuery)
	aHidden = ConvertStr(sHidden)
	
	'组织显示字段的SQL语句
	For i = 0 To UBound(aDisplay)
		If i = 0 Then
			sDispSQL = aDisplay(i).First
		Else
			sDispSQL = sDispSQL & ", " & aDisplay(i).First
		End If
	Next
	
	Response.Write "<table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""0"" class=""solid1"">" & _
		"<tr><td bgcolor=""#FFFFFF"">"
	Response.Write "<table width=""" & t_width & """ border=""" & t_border & """ cellspacing=""" & t_cellspacing & """ cellpadding=""" & t_cellpadding & """ bordercolor=""" & t_bordercolor & """ bgcolor=""" & t_bgcolor & """ background=""" & t_background & """ align=""" & t_align & """>"
	Response.Write "<tr><th bgcolor=""" & th_bgcolor & """ background=""" & th_backgound & """>请输入您要查询的关键字</th></tr>"
	Response.Write "<tr align=""center""><td bgcolor=""" & td_bgcolor & """>" 
	
	'显示可检索的文本输入框，并组织查询部分SQL语句
	'把显示和组织查询SQL部分语句写在一起，读起来有点累，但至少在一次循环里就搞定，效率提高不少
	For i = 0 To UBound(aQuery)
		Response.Write aQuery(i).Second & "：" & "<input size=""10"" type=""text"" name=""" & aQuery(i).First & """"
		iCount = 1
		'key是Request.QueryString集合里的字典名称
		'那么Request.QueryString(key)自然就是对应的值了
		For Each key In Request.QueryString
			sQueryString = Replace(Trim(Request.QueryString(key)),"'","''")
			If sQueryString <> Empty And key <> "Submit" And key <> "Query" And key <> "PageCount" And key <> "AbsolutePage" Then
				If iCount = 1 Then
					sQuerySQL = key & " LIKE '%" &sQueryString & "%'"
				Else
					sQuerySQL = sQuerySQL & " AND " & key & " LIKE '%" & sQueryString & "%'"
				End If
				iCount = iCount + 1
				If key = aQuery(i).First Then
					Response.Write " value=""" & Replace(sQueryString,"''","'") & """"
				End If
			End If
		Next
		Response.Write "> "
	Next
	Response.Write " <input name=""Submit"" type=""submit"" value=""查询"">"
	Response.Write "</td></tr></table>"
	Response.Write "</td></tr></table><br>"
	
	Dim iAbsolutePage
	iAbsolutePage = CInt(Request.QueryString("AbsolutePage"))
	
	'检查Submit按钮，并根据客户端提交的值，做不同的事务处理
	Select Case Request.QueryString("Submit")
		Case sHead
			iAbsolutePage = 1
		Case sPrevious
			iAbsolutePage = iAbsolutePage - 1
		Case sNext
			iAbsolutePage = iAbsolutePage + 1
		Case sFoot
			iAbsolutePage = CInt(Request.QueryString("PageCount"))
		Case sGo
			iAbsolutePage = CInt(Request.QueryString("AbsolutePage"))
		Case Else
			iAbsolutePage = 1
	End Select
	
	'构造查询SQL语句
	Dim strSQL
	strSQL = "SELECT Chat_Users.GUID As CGUID, Chat_Users.LastLogin As CLastLogin,"
	If iTop <> Empty Then strSQL = strSQL & " TOP " & iTop
	'If sHidden <> Empty Then strSQL = strSQL & " " & sHidden & ","
	If sDispSQL = Empty Then sDispSQL = "*"
	strSQL = strSQL & " " & sDispSQL
	strSQL = strSQL & " FROM " & sTableName
	If sQuerySQL <> Empty Then
		strSQL = strSQL & " WHERE " & sQuerySQL
	End If
	If sOrder <> Empty Then strSQL = strSQL & " ORDER BY " & sOrder
	'结束SQL构造工作
	'Response.Write strSQL
	'Response.End
	'创建一个ADO对象实例
	Set oADO = New ADO
	oADO.SetConnString(sConn)
	oADO.InitializeConnection
	'其实可以在SetConnString方法里就建立数据库连接的
	'Call oADO.SetADOVariables(2, 1, 1)
	'由于在ADO类初始化时，就给变量赋了值，上面一行可屏蔽，但为了ADO类的通用性，我保留了这个方法
	oADO.SetQueryString(strSQL)
		
	Dim j, rst, iFields, iPageCount
	Set rst = oADO.GetRecordset()
	
	Response.Write "<table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""0"" class=""solid1"">" & _
		"<tr><td bgcolor=""#FFFFFF"">"
	
	Response.Write "<table width=""" & t_width & """ height=""" & t_height & """ border=""" & t_border & """ cellspacing=""" & t_cellspacing & """ cellpadding=""" & t_cellpadding & """ bordercolor=""" & t_bordercolor & """ bgcolor=""" & t_bgcolor & """ background=""" & t_background & """ align=""" & t_align & """>"
	For i = 0 To UBound(aDisplay)
		'这里可以修改标题栏表格的样式
		If i = 7 Then Response.Write "<th bgcolor=""" & th_bgcolor & """ background=""" & th_backgound & """>最后登陆时间</th>"
		Response.Write "<th bgcolor=""" & th_bgcolor & """ background=""" & th_backgound & """>" & aDisplay(i).Second & "</th>"
	Next
	Dim bShowPage
	If rst.EOF Then
		Response.Write "<tr><td bgcolor=""" & td_bgcolor & """ colspan=""" & UBound(aDisplay) + 2 & """>找不到您要找的记录</td></tr></table>"
	Else
		bShowPage = True
		rst.PageSize = iPageSize
		iPageCount = rst.PageCount
		If iAbsolutePage > iPageCount Or iAbsolutePage < 1 Then
			rst.AbsolutePage = 1
		Else
			rst.AbsolutePage = iAbsolutePage
		End If

		iFields = rst.Fields.Count - 1
		j = 1
		Do
			Response.Write "<tr align=""" & tr_align & """ bgcolor=""" & tr_bgcolor & """>"
			For i = 0 To UBound(aDisplay)
				If i = 7 Then Response.Write "<td bgcolor=""" & td_bgcolor & """>" & rst("CLastLogin") & "</td>"
				If aDisplay(i).Replace = Empty Then
					If rst(aDisplay(i).First) = Empty Then
						Response.Write "<td width=""" & td_width & """ bgcolor=""" & td_bgcolor & """> </td>"
					Else
						Response.Write "<td bgcolor=""" & td_bgcolor & """>" & rst(aDisplay(i).First) & "</td>"
					End If
				Else
					Response.Write "<td bgcolor=""" & td_bgcolor & """>" & ReplaceRst(aHidden, ReplaceRst(aDisplay, aDisplay(i).Replace, rst), rst) & "</td>"
				End If
			Next
			Response.Write "</tr>"
			rst.MoveNext
			j = j + 1
			If rst.EOF Or j > iPageSize Then Exit Do
		Loop
		
		Response.Write "<tr align=""left""><td colspan=""" & iFields & """ bgcolor=""" & td_bgcolor & """>共" & rst.RecordCount & "条记录，每页显示" & iPageSize & "条，分" & rst.PageCount & "页显示，这是第" & iAbsolutePage & "页。</td></tr>"
		Response.Write "</table>"
	End If
	Response.Write "</td></tr>"
	
	If bShowPage Then
		Response.Write "<tr align=""right""><td bgcolor=""" & td_bgcolor & """>"
		'如果当前页是首页或者末尾，那么就把按钮改为button类型，可以有效的禁止用户提交表单
		If iAbsolutePage = 1 Then
			Response.Write "<input name=""Submit"" type=""button"" value=""" & sHead & """>"
			Response.Write "<input name=""Submit"" type=""button"" value=""" & sPrevious & """>"
		Else
			Response.Write "<input name=""Submit"" type=""submit"" value=""" & sHead & """>"
			Response.Write "<input name=""Submit"" type=""submit"" value=""" & sPrevious & """>"
		End If
		If iAbsolutePage = iPageCount Then
			Response.Write "<input name=""Submit"" type=""button"" value=""" & sNext & """>"
			Response.Write "<input name=""Submit"" type=""button"" value=""" & sFoot & """>"
		Else
			Response.Write "<input name=""Submit"" type=""submit"" value=""" & sNext & """>"
			Response.Write "<input name=""Submit"" type=""submit"" value=""" & sFoot & """>"
		End If
		
		Response.Write " <select name=""AbsolutePage"">"
		For i = 1 To iPageCount
			If i = iAbsolutePage Then
				Response.Write "<option value=""" & i & """ selected>第" & i & "页</option>"
			Else
				Response.Write "<option value=""" & i & """>第" & i & "页</option>"
			End If
		Next
		Response.Write "</select>"
		Response.Write "<input type=""Submit"" name=""Submit"" value=""" & sGo & """>"
		Response.Write "<input name=""PageCount"" type=""hidden"" value=""" & iPageCount & """>"
		Response.Write "</td></tr>"
	End If
	Response.Write "</table></form>"
	'释放记录集对象
	rst.Close
	Set rst = Nothing
	Set oADO = Nothing
End Function

'替换“$字段名$”为同名字段的数据
Function ReplaceRst(myarray, str, rst)
	Dim i
	For i = 0 To UBound(myarray)
		If Instr(str, "$") = 0 Then
			ReplaceRst = str
			Exit Function
		Else
			str = Replace(str, "$" & myarray(i).First & "$" , rst(myarray(i).First))
		End If
	Next
	ReplaceRst = str
End Function

'剥离字符串，创建变量为Twins类的数组的函数
Function ConvertStr(str)
	Dim i, sTempStr, sTempStr2, aArray
	
	aArray = Split(str, ",")
	'首先按","分隔符将str字符串劈分成数组

	For i = 0 To UBound(aArray)
		sTempStr = Trim(aArray(i))
		Set aArray(i) = New Twins
		'如果字符串中还有"|"，再将其依次转换成Twins对象的3个属性
		If Instr(sTempStr, "|") = 0 Then
			aArray(i).First = sTempStr
			aArray(i).Second = sTempStr
		Else
			aArray(i).First = Trim(Left(sTempStr, Instr(sTempStr, "|") - 1))
			sTempStr2 = Trim(Right(sTempStr, Len(sTempStr) - Instr(sTempStr, "|")))
			If Instr(sTempStr2, "|") = 0 Then
				aArray(i).Second = sTempStr2
			Else
				aArray(i).Second = Trim(Left(sTempStr2, Instr(sTempStr2, "|") - 1))
				aArray(i).Replace = Trim(Right(sTempStr2, Len(sTempStr2) - Instr(sTempStr2, "|")))
			End If
		End If
	Next
	ConvertStr = aArray
End Function

'一个可爱的Twins类，很解决问题
Class Twins
	Public First
	Public Second
	Public Replace
End Class
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>通用数据库显示程序</title>
<script src="../inc.js" language="javascript"></script>
<link rel="stylesheet" href="manager.css" type="text/css">
</head>
<body <%=g_BodyTag%>>
<%
'多个字段用数组表示
'DSN连接写法
'sConn = "DSN=<数据库名>;UID=<DSN账号>;PWD=<密码>"
'使用物理路径连接Access数据库写法
'sConn = "DBQ=" & Server.MapPath("<Access数据库文件的当前文件相对路径>") & ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
'连接SQLServer写法
'sConn = "Provider=SQLOLEDB;Persist Security Info=False;UID=<用户名>;PWD=<密码>;Initial Catalog=<SQLServer数据库名>;Data Source=<数据服务器IP>"

Dim sTableName, sHidden, sDisplay, sQuery, sOrder, iTop, iPageSize
sTableName = "Users INNER JOIN Chat_Users ON Users.GUID = Chat_Users.GUID"
sDisplay = "Portrait | 肖像 | <img src=""../$Portrait$"">,USERID | 用户名, NickName | 昵称 （GUID）| <a href=""m_vuser.asp?GUID=$CGUID$"">$NickName$($CGUID$)</a>, Sex|性别, Birthday|生日, ChatGroup|组, State|状态,Logins|登陆次数,ChatMinutes|聊天时长（分钟）,ChatWords|聊天总字节数"
sHidden = "CGUID"
sQuery = "USERID|用户名,Chat_Users.GUID|GUID, NickName|昵称, ChatGroup | 组"
sOrder = "Chat_Users.GUID"
'iTop = 1000
iPageSize = 20
DisplayTable g_DBString, sTableName, sHidden, sDisplay, sQuery, sOrder, iTop, iPageSize
%>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr> 
    <td class="Solid1" bgcolor="#CCCCCC">[<b>系统提示</b>] 
      <ol>
        <li>本页使用了“通用数据库显示页面”技术。智能生成查询分页程序，详情请访问<a href="http://www.xurui.com" target="_blank">http://www.xurui.com</a></li>
        </ol>
    </td>
  </tr>
</table>
<!-- #INCLUDE File = "../bottom.asp" -->
</body>
</html>

