<!--#include file="Config.asp" -->
<%
Dim SQL_Statement,RecordsAffected,RetRs,oField,Page_Count,TotalRec,i,abgcolor,CurPage,PCount,EndPage
'Dim Admin:Admin="SQL"
Response.Write "<table width='99%' border='0' cellspacing='1' cellpadding='3' align=center style='border-collapse:collapse'><tr><td colspan=2>SQL statement execution operation Note: This operation is limited to senior, more familiar to SQL programming, please consider carefully before operating your execute the statement is correct and complete, and after the execution can not be restored.</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=ds><th colspan=2><b>执行结果</b></th></tr><tr  class=ds><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=bk style='border-collapse: collapse'><tr  class=ds>"
							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=ds>"
								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=ds><td colspan="&RetRs.Fields.Count&" class=forumrow align=align>Page:"
							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=ds>"
							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=ds>"
								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=ds><th colspan=2><b>执行结果</b></th></tr><tr  class=ds><td align=center colspan=2>SQL语句有问题，具体错误信息如下：<br>"&Err.Description&"<br><br>[ <a href='javascript:history.go(-1)'>返回上一页</a> ]</td></tr></Table>"
				Err.clear
			End if
			On Error Goto 0
			Set RetRs=Nothing
		End If
	Else
		Response.Write "<Form Name=SQLForm Method=Post Action='?action=sql_done'><tr  class=ds><td align=center colspan=2><FieldSet><Legend>Please enter a SQL statement</Legend><textarea name='SQL_Statement' rows='6' cols='80'></textarea><p><Input type='Submit' Value=' EXECUTE '></p><br></FieldSet></td></tr></Form></Table>"
End If
Call CloseConn()
%></body></html>