<!--#include file="setup.asp"-->
<%
Dim i, j
Dim haveid
Set Rs = Server.CreateObject("ADODB.Recordset")
Server.ScriptTimeout = 9999999
NC_Admin.AdminChk = "64"
NC_Admin.Check
DownsysClass.admin_header
Select Case Trim(Request("Action"))
	Case "replace"
		Call ReplaceString
	Case "search"
		Call TableColumn
	Case "table"
		Call Tabletop
	Case Else
		Call ReplaceMain
End Select
If Founderr = True Then
	NC_Admin.Error_Msg (ErrMsg)
End If
DownsysClass.admin_footer
NothingObject

Private Sub ReplaceMain()
	Response.Write "<form name=""myform"" action="""" method=""post"">" & vbNewLine
	Response.Write " <table cellpadding=""3"" cellspacing=""1"" border=""0"" width=""100%"" class=""tableBorder"" align=center>" & vbNewLine
	Response.Write " <tr> " & vbNewLine
	Response.Write " <th height=""22"">数据库批量替换管理——选择数据表名</th>" & vbNewLine
	Response.Write " </tr>" & vbNewLine
	Response.Write " <tr> " & vbNewLine
	Response.Write " <td height=""25"" align=""center"" class=forumRow>请选择要替换的数据表名： " & vbNewLine
	Response.Write " <input name=""action"" type=""hidden"" value=""table"">" & vbNewLine
	Response.Write " <select name=""TableName"">" & vbNewLine
	Response.Write " "
	Set Rs = Conn.openSchema(20)
	While Not Rs.EOF
		Response.Write ("<option value='" & Rs(2) & "'>" & Rs(2) & "</option>")
		Rs.movenext
	Wend
	Response.Write " </select>" & vbNewLine
	Response.Write " <input type=""submit"" name=""Submit"" value=""下一步"" class=button>" & vbNewLine
	Response.Write " </td>" & vbNewLine
	Response.Write " </tr>" & vbNewLine
	Response.Write " <tr>" & vbNewLine
	Response.Write " <td class=forumRow align=""center"">请选择要替换的数据表</td>" & vbNewLine
	Response.Write " </tr>" & vbNewLine
	Response.Write " </table>" & vbNewLine
	Response.Write " </form>" & vbNewLine
End Sub


Private Sub Tabletop()
	Session("TableName") = DownsysClass.checkStr(Trim(Request.Form("TableName")))
	Response.Write "<form name=""myform"" action="""" method=""post"">" & vbNewLine
	Response.Write " <table cellpadding=""3"" cellspacing=""1"" border=""0"" width=""100%"" class=""tableBorder"" align=center>" & vbNewLine
	Response.Write " <tr> " & vbNewLine
	Response.Write " <th height=""22"">数据库批量替换管理——选择字段名输入查找内容</th>" & vbNewLine
	Response.Write " </tr>" & vbNewLine
	Response.Write " <tr> " & vbNewLine
	Response.Write " <td height=""25"" align=""center"" class=forumRow>要替换的数据表名： " & vbNewLine
	Response.Write " <select name=""TableName"">" & vbNewLine
	Response.Write " <option value="""
	Response.Write Session("TableName")
	Response.Write """ selected>"
	Response.Write Session("TableName")
	Response.Write "</option>" & vbNewLine
	Response.Write " </select>" & vbNewLine
	Response.Write " </td>" & vbNewLine
	Response.Write " </tr>" & vbNewLine
	Response.Write " <tr> " & vbNewLine
	Response.Write " <td height=""25"" align=""center"" class=forumRow>要替换的字段名： " & vbNewLine
	Response.Write " <select name=""ColumnName"">" & vbNewLine
	Response.Write " "
	haveid = 0
	Set Rs = CreateObject("adodb.recordset")
	SQL = "select * from [" & Session("TableName") & "] where 1<>1"
	Rs.Open SQL, Conn, 1, 1
	j = Rs.Fields.Count
	For i = 0 To (j - 1)
		Response.Write ("<option value='" & Rs.Fields(i).Name & "'>" & Rs.Fields(i).Name & "</option>")
		If LCase(Rs.Fields(i).Name) = "id" Then
			Session("haveid") = 1
		End If
	Next
	Rs.Close
	Response.Write " </select>" & vbNewLine
	Response.Write " </td>" & vbNewLine
	Response.Write " </tr>" & vbNewLine
	Response.Write " <tr> " & vbNewLine
	Response.Write " <td height=""25"" align=""center"" class=forumRow>字段名中包含的字符： " & vbNewLine
	Response.Write " <input name=""action"" type=""hidden"" value=""search"">" & vbNewLine
	Response.Write " <input name=""oldString"" type=""text"" size=""45"">" & vbNewLine
	Response.Write " <input type=""submit"" name=""Submit"" value=""开始查找"" class=button>" & vbNewLine
	Response.Write " </td>" & vbNewLine
	Response.Write " </tr>" & vbNewLine
	Response.Write " <tr>" & vbNewLine
	Response.Write " <td class=forumRow align=""center""><a href=""javascript:history.go(-1)"" >&lt;&lt; 返回上一页</a></td>" & vbNewLine
	Response.Write " </tr>" & vbNewLine
	Response.Write " <tr> " & vbNewLine
	Response.Write " <td height=""25"" align=""center"" class=forumRow>注意：单引号“'”将被自动过滤掉</td>" & vbNewLine
	Response.Write " </tr>" & vbNewLine
	Response.Write " </table>" & vbNewLine
	Response.Write " </form>" & vbNewLine
End Sub


Private Sub TableColumn()
	Response.Write " <form name=""myform"" action="""" method=""post"">" & vbNewLine
	Response.Write " <table cellpadding=""3"" cellspacing=""1"" border=""0"" width=""100%"" class=""tableBorder"" align=center>" & vbNewLine
	Response.Write " <tr> " & vbNewLine
	Response.Write " <th height=""22"">数据库批量替换管理——替换" & vbNewLine
	Response.Write "<input name=""action"" type=""hidden"" value=""replace""></th>" & vbNewLine
	Response.Write " </tr>" & vbNewLine
	Response.Write " <tr> " & vbNewLine
	Response.Write " <td height=""25"" align=""center"" class=forumRow> " & vbNewLine
	Session("ColumnName") = DownsysClass.checkStr(Trim(Request.Form("ColumnName")))
	Set Rs = CreateObject("adodb.recordset")
	SQL = "select count(" & Session("ColumnName") & ") from " & Session("TableName") & " where " & Session("ColumnName") & " like '%" & DownsysClass.checkStr(Trim(Request.Form("oldString"))) & "%'"
	Rs.Open SQL, Conn, 1, 1
	If Not (Rs.EOF And Rs.bof) Then
		Session("oldString") = DownsysClass.checkStr(Trim(Request.Form("oldString")))
		Response.Write ("本次搜索找到了 <b>" & Rs(0) & "</b> 个相关字符串。")
		Response.Write ("<a href=""javascript:history.go(-1)"">返回重新查找</a>")
	Else
		Response.Write ("没有找到相关字符串，<a href=""javascript:history.go(-1)"">返回重新查找</a>")
	End If
	Rs.Close
	Set Rs = Nothing
	Response.Write " </td>" & vbNewLine
	Response.Write " </tr>" & vbNewLine
	Response.Write " <tr> " & vbNewLine
	Response.Write " <td height=""25"" align=""center"" class=forumRow>将字符 " & vbNewLine
	Response.Write " <input disabled name=""oldString"" type=""text"" value="""
	Response.Write Trim(Request.Form("oldString"))
	Response.Write """ size=""45""> " & vbNewLine
	Response.Write " </td>" & vbNewLine
	Response.Write " </tr>" & vbNewLine
	Response.Write " <tr> " & vbNewLine
	Response.Write " <td height=""25"" align=""center"" class=forumRow>替换成 " & vbNewLine
	Response.Write " <input name=""newString"" type=""text"" value="""" size=""45""> " & vbNewLine
	Response.Write " </td>" & vbNewLine
	Response.Write " </tr>" & vbNewLine
	Response.Write " <tr> " & vbNewLine
	Response.Write " <td height=""25"" align=""center"" class=forumRow>" & vbNewLine
	Response.Write "<input type=""submit"" name=""Submit2"" value=""开始替换"" class=button></td>" & vbNewLine
	Response.Write " </tr>" & vbNewLine
	Response.Write " <tr>" & vbNewLine
	Response.Write " <td class=forumRow align=""center""><a href=""javascript:history.go(-1)"" >&lt;&lt; 返回上一页</a></td>" & vbNewLine
	Response.Write " </tr>" & vbNewLine
	Response.Write " </table>" & vbNewLine
	Response.Write " </form>" & vbNewLine
End Sub


Private Sub ReplaceString()
	Dim oldString
	Dim newString
	Dim TableName
	Dim ColumnName
	Dim RepString
	Dim uprs
	oldString = DownsysClass.checkStr(Trim(Session("oldString")))
	newString = DownsysClass.checkStr(Trim(Request.Form("newString")))
	TableName = DownsysClass.checkStr(Trim(Session("TableName")))
	ColumnName = DownsysClass.checkStr(Trim(Session("ColumnName")))
	Set Rs = CreateObject("adodb.recordset")
	Set uprs = CreateObject("adodb.recordset")
	haveid = Session("haveid")
	If haveid = 1 Then
		SQL = "select id," & Session("ColumnName") & " from " & Session("TableName") & " where " & Session("ColumnName") & " like '%" & Trim(Session("oldString")) & "%'"
		Rs.Open SQL, Conn, 1, 1
		If Not (Rs.EOF And Rs.bof) Then
			i = Rs.recordcount
			Do While Not Rs.EOF
				RepString = Replace(Rs(1), "" & oldString & "", "" & newString & "")
				SQL = "select * from " & TableName & " where ID=" & Rs(0)
				uprs.Open SQL, Conn, 1, 3
				uprs("" & ColumnName & "") = RepString
				uprs.Update
				uprs.Close
				Rs.movenext
			Loop
		End If
	Else
		SQL = "select " & Session("ColumnName") & " from " & Session("TableName") & " where " & Session("ColumnName") & " like '%" & Trim(Session("oldString")) & "%'"
		Rs.Open SQL, Conn, 1, 1
		If Not (Rs.EOF And Rs.bof) Then
			i = Rs.recordcount
			Do While Not Rs.EOF
				RepString = Replace(Rs(0), "" & oldString & "", "" & newString & "")
				SQL = "select * from " & ColumnName & " where " & ColumnName & "='" & Rs(0) & "'"
				uprs.Open SQL, Conn, 1, 3
				uprs("" & ColumnName & "") = RepString
				uprs.Update
				uprs.Close
				Rs.movenext
			Loop
		End If
	End If
	Rs.Close
	Set uprs = Nothing
	Set Rs = Nothing
	NC_Admin.Succeed_Msg ("<li>批量替换操作成功，共更新了 " & i & " 条信息！</li>")
End Sub
%>
