<!--#include file="../inc/Conn.asp"-->
<!--#include file="Inc/Admin.Asp"-->
<!--#include file="Inc/Head.Asp"-->
<%
PurviewValue="Admin_Sql"
If not CheckAdmin(PurviewValue) then
Response.Write("<script language=""JavaScript"">alert(""越权操作!"");history.go(-1);")
response.end
End If
%>
<%
Const NeedCheckComeUrl = True   '是否需要检查外部访问

Const PurviewLevel = 2      '0--不检查，1--超级管理员，2--普通管理员
Const PurviewLevel_Channel = 1   '0--不检查，1--频道管理员，2--栏目总编，3--栏目管理员
Const PurviewLevel_Others = ""   '其他权限
AdminTitle="数据库Sql管理"
call main()
Sub main()
Dim SQL_Statement,RecordsAffected,RetRs,oField,Page_Count,TotalRec,i,abgcolor,CurPage,PCount,EndPage
Response.Write "<table width='100%' border='0' align='center' cellpadding='2' cellspacing='1' class='border'>" & vbCrLf
Response.write "<tr  class=Title><th colspan=2><b>高级sql管理</b></th></tr>"
Response.write "<tr class=tdbg><td align=right Width=200><b>SQL语句执行操作：</b></td><td><blockquote><br>*****本操作仅限高级、对SQL编程比较熟悉的用户，您可以直接输入SQL执行语句，比如select * from MS_User，查询会员信息。<br><font color=#ff0000>在操作前请慎重考虑您的执行语句是否正确和完整，执行后不可恢复。</Font></blockquote></td></tr>"
	If Request("action") = "sql_done" Then
		SQL_Statement=Request("SQL_Statement")
		If SQL_Statement<>"" then
			On Error Resume Next
			Server.ScriptTimeOut=999999
			Set RetRs=Conn.Execute(SQL_Statement,RecordsAffected)
			If Err.number="0" then
				On Error Goto 0
				Response.write "<tr  class=Title><th colspan=2><b>执行结果</b></th></tr><tr  class=tdbg><td align=center colspan=2>"
				If RetRs.State=0 Then
					If RecordsAffected>0 Then
						Response.write "执行成功，共有 "&RecordsAffected&" 条记录被处理。<br><br>[ <a href=# onclick=history.go(-1)>返回上一页</a> ]</td></tr></Table>"
					Else
						Response.write "执行成功，没有任何记录被处理。<br><br>[ <a href=# onclick=history.go(-1)>返回上一页</a> ]</td></tr></Table>"
					End If
				Else
					RetRs.Close
					RetRs.Open SQL_Statement,Conn,1,1
					If RetRs.Bof And RetRs.Eof Then
						Response.write "执行成功，没有任何记录被返回。<br><br>[ <a href=# onclick=history.go(-1)>返回上一页</a> ]</td></tr></Table>"
					Else
						CurPage=request("page")
						If CurPage="" or not IsNumeric(CurPage) Then
							CurPage=1
						Else
							CurPage=CLng(CurPage)
						End If
						'删除语句
						If RetRs.RecordCount>0 Then
							RetRs.AbsolutePage=CurPage
							Page_Count=0
							TotalRec=RetRs.RecordCount
							Response.write "执行成功，共返回 "&TotalRec&" 条记录。<br><br>[ <a href=# onclick=history.go(-1)>返回上一页</a> ]</td></tr></Table>"
							Response.Write "<br><table width=95% border='1' cellspacing='1' cellpadding='3' align=center class=Hxcmsbk style='border-collapse: collapse'><tr  class=tdbg>"
							For Each oField In RetRs.Fields
								Response.Write "<Th>" & oField.Name &"</Th>"
							Next
							Do While (not RetRs.eof) And (not Page_Count = Cint(RetRs.PageSize))
								Response.Write "<tr  class=tdbg>"
								If Page_Count mod 2 = 0 Then
									abgcolor="ForumRow"
								Else
									abgcolor="ForumRowHilight"
								End If
								For i=0 To RetRs.Fields.Count-1
									Response.write "<td class="&abgcolor&">"
									If IsNull(RetRs(i)) Then
										Response.Write ""
									Else
										Response.Write Server.HTMLEncode(RetRs(i))
									End If
									Response.write "</td>"
								Next
								Response.Write "</tr>"
								Page_count = Page_Count + 1
								RetRs.MoveNext
							Loop
							Response.Write "<tr  class=tdbg><td colspan="&RetRs.Fields.Count&" class=forumrow align=align>分页："
							Pcount=RetRs.PageCount
							If CurPage > 4 Then
								Response.write "<a href=""?page=1&action=sql_done&sql_statement="&SQL_Statement&""">[1]</a> ..."
							End If
							If PCount>CurPage+3 Then
								EndPage=CurPage+3
							Else
								EndPage=PCount
							End If
							For i=CurPage-3 To EndPage
								If i>=1 Then
									If i = CLng(CurPage) Then
										Response.write " <font color=red>["&i&"]</font>"
									Else
										Response.write " <a href=""?page="&i&"&action=sql_done&sql_statement="&SQL_Statement&""">["&i&"]</a>"
									End If
								End If
							Next
							If CurPage+3 < PCount Then 
								Response.write "... <a href=""?page="&PCount&"&action=sql_done&sql_statement="&SQL_Statement&""">["&PCount&"]</a>"
							End If
						Else
							Response.write "执行成功，返回如下记录：<br><br>[ <a href=# onclick=history.go(-1)>返回上一页</a> ]</td></tr></Table>"
							Response.Write "<br><table width=95% border='0' cellspacing='1' cellpadding='3' align=center Class=TableBorder><tr  class=tdbg>"
							For Each oField In RetRs.Fields
								If oField.Name<>"" Then
									Response.Write "<Th>" & oField.Name &"</Th>"
								Else
									Response.Write "<Th>执行结果：</Th>"
								End If
							Next
							Do While not RetRs.eof
								Response.Write "<tr  class=tdbg>"
								If Page_Count mod 2 = 0 Then
									abgcolor="ForumRow"
								Else
									abgcolor="ForumRowHilight"
								End If
								For i=0 To RetRs.Fields.Count-1
									Response.write "<td class="&abgcolor&">"
									If IsNull(RetRs(i)) Then
										Response.Write ""
									Else
										Response.Write RetRs(i)
									End If
									Response.write "</td>"
								Next
								Response.Write "</tr>"
								RetRs.MoveNext
							Loop
						End If
						Response.Write "</table></td></tr></Table>"
					End If
					RetRs.Close
				End If
			Else
				Response.write "<tr  class=tdbg><th colspan=2><b>执行结果</b></th></tr><tr  class=tdbg><td align=center colspan=2>SQL语句有问题，具体错误信息如下：<br>"&Err.Description&"<br><br>[ <a href=# onclick=history.go(-1)>返回上一页</a> ]</td></tr></Table>"
				Err.clear
			End if
			On Error Goto 0
			Set RetRs=Nothing
		End If
	Else
		Response.Write "<tr  class=tdbg><th colspan=2><b></b>请输入SQL语句</th></tr><Form Name=SQLForm Method=Post Action='?action=sql_done'><tr  class=tdbg><td align=center colspan=2><FieldSet><Legend>请输入SQL语句</Legend>指令：<Input type='text' name='SQL_Statement' Size=60><br><Input type='Submit' Value=' 执  行 '></FieldSet></td></tr></Form></Table>"
	End If
End Sub
%>