<!--#include file="Admin_Const.asp"-->
<%
if Not Cl.TrueOtherPurview("Data") then
	Admin_ShowErr("<br><li>您无此操作权限！</li>")
end if
%>
<html>
<head>
<title>创力数据库内容替换程序</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="../skins/admin_STYLE.CSS" rel="stylesheet" type="text/css">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<%
if trim(request("type1"))="step001" then
	Call ShowChird()
else
%>
<table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="border">
	<tr class="title"> 
		<td height="22" colspan="2">数 据 库 内 容 替 换 程 序</td>
	</tr>
	<tr class="tdbg">
		<td width="70" height="30" ><strong>管理导航：</strong></td>
		<td><a href="Admin_Replace.asp">数据库内容批量替换（普通模式）</a>&nbsp;|&nbsp;<a href="Admin_Replace.asp?Action=Main2">数据库内容批量替换（高级模式）</a>&nbsp;|&nbsp;<a href="Admin_Replace.asp?Action=Main2">数据库内容批量替换（赋值模式）</a></td></tr>
</table>
<%
	dim Action
	i=0
	Action=trim(request("action"))
	Select Case Action
	Case "Replace1","Replace2","Replace3"
		call step1()
	Case "Main2"
		call Main2()
	Case "Main3"
		call Main3()
	Case Else
		call Main1()
	End Select
end if
%>
</body>
</html>
<%
CloseAllObj



Sub Main1()
%>
<script language = "JavaScript">
function changedb(){
    tablechird.location.href="Admin_Replace.asp?type1=step001&type="+document.myform.TableName.value;
}
function CheckForm(){
  if (document.myform.TableName.value==''){
    alert('数据表名不能为空！');
    document.myform.TableName.focus();
    return false;
  }
  if (document.myform.ColumnName.value==''){
    alert('字段名不能为空！');
    //document.myform.ColumnName.focus();
    return false;
  }
  if (document.myform.strOld.value==''){
    alert('替换字符不能为空！');
    document.myform.strOld.focus();
    return false;
  }
  return true;  
}
</script>
<form method="post" name="myform" onSubmit="return CheckForm();" action="Admin_Replace.asp" target="_self">
<table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="border">
	<tr class="title"><td height="22" colspan="2" align="center"><strong>数 据 库 内 容 批 量 替 换（普通模式）</strong></td></tr>
	<tr class="tdbg"><td height="40" align="right" width=80>数据表名：</td><td height="40"><%Call ShowMain()%><font color=red> *</font></td></tr>
	<tr class="tdbg"><td height="40" align="right">字 段 名：</td><td height="40"><input name="ColumnName" type="hidden" value="" size="40"><iframe style="top:2px;" ID="tablechird" src="Admin_Replace.asp?type1=step001" frameborder=0 scrolling=no width="100%" height="20"></iframe></td></tr>
	<tr class="tdbg"><td height="40" align="right">其它条件：</td><td height="40"><input name="OtherSql" type="text" value="" size="72">(一般不需填写)</td></tr>
	<tr class="tdbg"><td height="40" align="right">替换数量：</td><td height="40"><input name="thnum" type="text" value="" size="72"> 替换多少条数据,0或不填写为不限</td></tr>
	<tr class="tdbg"><td height="40" align="right">将 字 符：</td><td height="40"><textarea name="strOld" cols="60" rows="4"></textarea><font color=red> *</font></td></tr>
	<tr class="tdbg"><td height="40" align="right">替 换 成：</td><td height="40"><textarea name="strNew" cols="60" rows="4"></textarea></td></tr>
	<tr class="tdbg"><td height="40" align="right">注意事项：</td><td height="40">	1、请备份您的创力数据库文件。<br>2、本操作的更新时间视您数据的多少以及服务器（或本地机器）的配置决定，如果数据很多，更新可能很慢，在这过程千万不能刷新页面或关闭浏览器，如果出现超时或者错误提示，请使用备份数据重新进行操作。</td></tr>
	<tr class="tdbg"><td height="40" colspan="2" align="center"><input type="hidden" name="Action" value="Replace1"><input type="submit" name="Submit" value="开始替换"></td></tr>
</table>
</form>
<%
End Sub

Sub Main2()
%>
<script language = "JavaScript">
function changedb(){
    tablechird.location.href="Admin_Replace.asp?type1=step001&type="+document.myform.TableName.value;
}
function CheckForm1(){
  if (document.myform.TableName.value==''){
    alert('数据表名不能为空！');
    document.myform.TableName.focus();
    return false;
  }
  if (document.myform.ColumnName.value==''){
    alert('字段名不能为空！');
    //document.myform.ColumnName.focus();
    return false;
  }
  if (document.myform.strOld.value==''){
    alert('替换开始代码不能为空！');
    document.myform.strOld.focus();
    return false;
  }
   if (document.myform.strOld1.value==''){
    alert('替换结束代码不能为空！');
    document.myform.strOld1.focus();
    return false;
  }
  return true;  
}
</script>
<form method="post" name="myform" onSubmit="return CheckForm1();" action="Admin_Replace.asp" target="_self">
<table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="border">
	<tr class="title"><td height="22" colspan="2" align="center"><strong>数 据 库 内 容 批 量 替 换 （高级模式）</strong></td></tr>
	<tr class="tdbg"><td height="40" align="right" width=80><b>特别事项</b>：</td><td height="40"><font color=red>高级模式较易出错，且替换速度较慢、较占服务器CPU和内存资源,请谨慎使用!如无特殊需要,建议使用普通模式!</font></td></tr>
	<tr class="tdbg"><td height="40" align="right" width=80>数据表名：</td><td height="40"><%Call ShowMain()%><font color=red> *</font></td></tr>
	<tr class="tdbg"><td height="40" align="right">字 段 名：</td><td height="40"><input name="ColumnName" type="hidden" value="" size="40"><iframe style="top:2px;" ID="tablechird" src="Admin_Replace.asp?type1=step001" frameborder=0 scrolling=no width="100%" height="20"></iframe></td></tr>
	<tr class="tdbg"><td height="40" align="right">其它条件：</td><td height="40"><input name="OtherSql" type="text" value="" size="72">(一般不需填写)</td></tr>
	<tr class="tdbg"><td height="40" align="right">替换数量：</td><td height="40"><input name="thnum" type="text" value="" size="72"> 替换多少条数据,0或不填写为不限</td></tr>
	<tr class="tdbg"><td height="40" align="right">字符开始：</td><td height="40"><textarea name="strOld" cols="60" rows="3"></textarea><font color=red> *</font></td></tr>
	<tr class="tdbg"><td height="40" align="right">字符结束：</td><td height="40"><textarea name="strOld1" cols="60" rows="3"></textarea><font color=red> *</font></td></tr>
	<tr class="tdbg"><td height="40" align="right">替 换 成：</td><td height="40"><textarea name="strNew" cols="60" rows="3"></textarea></td></tr>
	<tr class="tdbg"><td height="40" align="right">注意事项：</td><td height="40">	1、请备份您的创力的数据库文件。<br>2、本操作的更新时间视您数据的多少以及服务器（或本地机器）的配置决定，如果数据很多，更新可能很慢，在这过程千万不能刷新页面或关闭浏览器，如果出现超时或者错误提示，请使用备份数据重新进行操作。</td></tr>
	<tr class="tdbg"><td height="40" colspan="2" align="center"><input type="hidden" name="Action" value="Replace2"><input type="submit" name="Submit" value="开始替换"></td></tr>
</table>
</form>
<%
End Sub

Sub Main3()
%>
<script language = "JavaScript">
function changedb(){
    tablechird.location.href="Admin_Replace.asp?type1=step001&type="+document.myform.TableName.value;
}
function CheckForm1(){
  if (document.myform.TableName.value==''){
    alert('数据表名不能为空！');
    document.myform.TableName.focus();
    return false;
  }
  if (document.myform.ColumnName.value==''){
    alert('字段名不能为空！');
    //document.myform.ColumnName.focus();
    return false;
  }
  return true;  
}
</script>
<form method="post" name="myform" onSubmit="return CheckForm1();" action="Admin_Replace.asp" target="_self">
<table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="border">
	<tr class="title"><td height="22" colspan="2" align="center"><strong>数 据 库 内 容 批 量 替 换 （赋值模式）</strong></td></tr>
	<tr class="tdbg"><td height="40" align="right" width=80><b>特别事项</b>：</td><td height="40"><font color=red>赋值模式不理会原有数据,而是把选定字段的值直接修改为新的值,这会导致原数据丢失并变为新数据!请谨慎使用!</font></td></tr>
	<tr class="tdbg"><td height="40" align="right" width=80>数据表名：</td><td height="40"><%Call ShowMain()%><font color=red> *</font></td></tr>
	<tr class="tdbg"><td height="40" align="right">字 段 名：</td><td height="40"><input name="ColumnName" type="hidden" value="" size="40"><iframe style="top:2px;" ID="tablechird" src="Admin_Replace.asp?type1=step001" frameborder=0 scrolling=no width="100%" height="20"></iframe></td></tr>
	<tr class="tdbg"><td height="40" align="right">其它条件：</td><td height="40"><input name="OtherSql" type="text" value="" size="72">(一般不需填写)</td></tr>
	<tr class="tdbg"><td height="40" align="right">修改数量：</td><td height="40"><input name="thnum" type="text" value="" size="72"> 替换多少条数据,0或不填写为不限</td></tr>
	<tr class="tdbg"><td height="40" align="right">修 改 成：</td><td height="40"><textarea name="strNew" cols="60" rows="3"></textarea></td></tr>
	<tr class="tdbg"><td height="40" align="right">注意事项：</td><td height="40">	1、请备份您的创力的数据库文件。<br>2、本操作的更新时间视您数据的多少以及服务器（或本地机器）的配置决定，如果数据很多，更新可能很慢，在这过程千万不能刷新页面或关闭浏览器，如果出现超时或者错误提示，请使用备份数据重新进行操作。</td></tr>
	<tr class="tdbg"><td height="40" colspan="2" align="center"><input type="hidden" name="Action" value="Replace3"><input type="submit" name="Submit" value="开始替换"></td></tr>
</table>
</form>
<%
End Sub


'**************************************************
'过程名：Step1
'作  用：数据替换主调用程序
'参  数：无
'**************************************************
Sub Step1()
	dim rs,sql
	BeginTime=timer
	dim TableName,ColumnName,OtherSql,strOld,strOld1,strNew,thnum
	TableName	= Cl.ReplaceBadChar(trim(request("TableName")))
	ColumnName	= Cl.ReplaceBadChar(trim(request("ColumnName")))
	OtherSql	= trim(request("OtherSql"))
	strOld		= trim(request("strOld"))
	strOld1		= trim(request("strOld1"))
	strNew		= trim(request("strNew"))
	thnum		= trim(request("thnum"))
	if thnum="" then
		thnum=0
	End if
	if TableName="" then
		response.write "请输入要替换的数据表名！"
		exit sub
	end if
	if ColumnName="" then
		response.write "请输入要替换的字段名！"
		exit sub
	end if
	if action="Replace1" then
		if strOld="" then
			response.write "请输入要替换的字符！"
			exit sub
		end if
	else 
		if action="Replace2" then
			if strOld="" then
				response.write "请输入要替换的字符开始代码！"
				exit sub
			end if
			if strOld1="" then
				response.write "请输入要替换的字符结束代码！"
				exit sub
			end if
		End if		
	End if
	on error resume next
	Set rs = Server.CreateObject("ADODB.Recordset")
	if thnum<>0 then
		sql = "select Top " & thnum & " " & ColumnName & "  from " & TableName
	else
		sql = "select " & ColumnName & "  from " & TableName
	End if
	if OtherSql<>"" then
	  sql = sql & " where " & OtherSql
	end if
	OpenConn : rs.open sql,conn,1,1
	if err.number<>0 then
		response.write "<font color=red>数据库操作失败，请检查数据表名和字段名添写是否正确</font>"
		exit sub
	end if
	set rs=nothing
	on error GoTo 0
	response.write "正在替换有关数据……<font color=red>在此过程中请勿刷新页面或关闭窗口！</font><br>"
	call ReplaceData(TableName,ColumnName,OtherSql,strOld,strOld1,strNew,thnum)	
	EndTime=timer	

    Response.Write "<table cellpadding=2 cellspacing=1 border=0 width=400 class='border' align=center><tr align='center' class='title'><td height='22'><strong>恭喜你！</strong></td></tr>" & vbCrLf
    Response.Write "  <tr class='tdbg'><td height='100' valign='top'><br>" & "共替换：<font color='#0000ff'>"&i&"</font> 项数据。<br>共耗时：<font color='#0000ff'>"&FormatNumber((EndTime-BeginTime)*1000,2)&"</font> 毫秒。" & "</td></tr>" & vbCrLf
    Response.Write "  <tr align='center' class='tdbg'><td><a href='#' onclick=history.go(-1) >&lt;&lt; 返回上一页</a></td></tr></table>"& vbCrLf

End Sub

'**************************************************
'过程名：ReplaceData
'作  用：显示数据表列表
'参  数：TableName	----数据表名
'        ColumnName	----字段名
'        OtherSql	----其他条件
'        strOld		----查找字符(或查找开始代码)
'        strOld1	----查找结束代码
'        strNew		----替换字符
'        thnum		----替换项目数
'**************************************************
Sub ReplaceData(TableName,ColumnName,OtherSql,strOld,strOld1,strNew,thnum)
	dim rs,sql,tt
	Response.Write "<li>正在替换数据</li>&nbsp;&nbsp;"
	Set rs = Server.CreateObject("ADODB.Recordset")
	if thnum<>0 then
		sql = "select top " & thnum & " " & ColumnName & "  from " & TableName 
	else
		sql = "select " & ColumnName & "  from " & TableName 
	End if
	if OtherSql<>"" then
	  sql = sql & " where " & OtherSql
	end if
	rs.open sql,conn,1,3
	
	Select Case Action
	Case "Replace1"
		do while not rs.eof
			if instr(rs(ColumnName),strOld)<>0 then
				rs(ColumnName)=replace(rs(ColumnName),strOld,strNew)
				i=i+1
				Response.Write "."
			end if
			rs.update
			rs.movenext
		loop
	Case "Replace2"
		Set tt=new RegExp
  		tt.IgnoreCase =true
  		tt.Global=True
  		tt.Pattern=strOld&"[^"&strOld1&"]*"&strOld1
		do while not rs.eof
 			rs(ColumnName) = tt.Replace(rs(ColumnName),strNew) 
  			i=i+1
			Response.Write "."
			rs.update
			rs.movenext
		loop
		Set tt=Nothing
	Case "Replace3"
		do while not rs.eof
			Response.Write strNew
			rs(ColumnName)=strNew
			i=i+1
			Response.Write "."
			rs.update
			rs.movenext
		loop
	End Select
	rs.close
	set rs=nothing
	response.write "&nbsp;&nbsp;<font color='#009900'>替换数据成功！</font>"
End Sub

'**************************************************
'过程名：ShowMain
'作  用：显示数据表列表
'参  数：无
'**************************************************
Sub ShowMain()
	dim rs,tablename,temptable
	OpenConn : Set rs = Conn.OpenSchema(4)
	tablename=""
	response.write "<select name='TableName' onChange='changedb()'><option value=''>请选择一个数据表</option>"
	Do Until rs.EOF
		temptable=rs("Table_name")
		if temptable <> tablename and temptable <> "Cl_Admin" and temptable <> "Cl_NotDownload" and temptable <> "MSysAccessXML" and temptable <> "MSysAccessObjects" then
			Response.write "<option value='" & temptable & "'>" & temptable & "</option>"
			Tablename = temptable
		end if
	rs.MoveNext
	Loop
	response.write "</select>"
	rs.close:set rs=nothing
End Sub

'**************************************************
'过程名：ShowChird
'作  用：显示指定数据表的字段列表
'参  数：无
'**************************************************
Sub ShowChird()
	dim rs
	response.write "<body class='tdbg'><form method='post' name='myform11' action='Admin_Replace.asp'><select name='dbname2' onChange=parent.document.myform.ColumnName.value=document.myform11.dbname2.value><option value=''>请选择一个字段　</option>"
	if trim(request("type"))<>"" then	
		OpenConn : Set rs = Conn.OpenSchema(4)	
		Do Until rs.EOF or rs("Table_name") = trim(request("type"))
			rs.MoveNext
		Loop
		Do Until rs.EOF or rs("Table_name") <> trim(request("type"))
			response.write "<option value='"&rs("column_Name")&"'>"&rs("column_Name")&"</option>"
			rs.MoveNext
		loop
		rs.close:set rs=nothing
	End if
	response.write "</select><font color=red> *</font></form><script language = 'JavaScript'>parent.document.myform.ColumnName.value=document.myform11.dbname2.value;</script></body>"
End Sub
%>