<!--#include file="../inc/config.asp"-->
<!--#include file="../inc/conn.asp"-->
<!-- #include file="inc/Session.asp" -->
<!-- #include file="inc/function.asp" -->
<!-- #include file="inc/checkrole.asp" -->
<%
' 如果数据库很大，请设置以下的时间，单位秒
Server.ScriptTimeout = 999999
'''''''''''''''''''''''''''''''''''''''''''''''
call checkPrevilege(Session("Name"),"p09")
'''''''''''''''''''''''''''''''''''''''''''''''
dim id,taskArry,BeginTime,catalogSaveDir,catalogDomainUrl
BeginTime=Timer
current_time=now()
if(request.querystring("id")<>"") then
	id=Clng(request.querystring("id"))
else
	call alertstr("没有记录",1) 
end if
set rs=server.createobject("adodb.recordset")
rs.open "select db_type,db_host,db_name,db_username,db_password,db_table,db_key,target_catalog,templetid,news_status,field_map ,db_order,db_numlimit from t_importdb where id="&id,conn,3,1
if not rs.eof then
	taskArry=rs.GetRows()
else
	call alertstr("没有记录",1) 
end if
rs.close
set rs=nothing
'读取栏目设置
set rs=server.CreateObject("adodb.recordset")
rs.open "select savedir,domainurl from catalogs where id="&taskArry(7,0),conn,3,1
if not rs.eof then
	catalogSaveDir=rs(0)
	catalogDomainUrl=rs(1)
end if 
rs.close
set rs=nothing
%>
<html>
<head>
<title>数据库采集</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="style/global.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style5 {
	font-size: 12px;
	color: #FFFFFF;
	font-weight: bold;
}
-->
</style></head>
<body>
<table width="100%"  border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><iframe src="right_top.htm" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" height="30" width="100%"></iframe></td>
  </tr>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#666666">
  <tr>
    <td height="22" bgcolor="#FF9900"><div align="center"><span class="style5">采集数据库中的信息</span></div></td>
  </tr>
</table>
<p>&nbsp;</p>
<p align="center">正在采集...</p>
<%
dim db_conn,db_connstr,fields_map,from_sql,from_rows,offset,totalNum,percentNum
offset=0
if(taskArry(0,0)=0) then
		db_connstr="driver={Microsoft Access Driver (*.mdb)};dbq="&taskArry(1,0)&";uid="&taskArry(3,0)&";pwd="&taskArry(4,0)&";"
elseif(taskArry(0,0)=1) then
		db_connstr="driver={sql server};server="&taskArry(1,0)&";database="&taskArry(2,0)&";uid="&taskArry(3,0)&";pwd="&taskArry(4,0)&";"
end if
on error resume next
set db_conn=server.CreateObject("adodb.connection") 
db_conn.open db_connstr
if Err.Number <> 0 then
	Response.Write("<script>alert('无法连接至源数据库!请检查设置是否正确.');history.back();</script>")  
    Response.End
end if
fields_map=split(taskArry(10,0),"||")
redim from_field_arr(ubound(fields_map))
redim target_field_arr(ubound(fields_map))
from_sql="select "
if(Cint(taskArry(12,0))<>0) then
	from_sql=from_sql&" top "&taskArry(12,0)&" "
end if
for i=0 to ubound(fields_map)
	fields_arr=split(fields_map(i),"<>")
	from_field_arr(i)=fields_arr(0)
	target_field_arr(i)=fields_arr(1)
	from_sql=from_sql&from_field_arr(i)	
	if(i<ubound(fields_map)) then
		from_sql=from_sql&","		
	end if	
next
from_sql=from_sql&" from "&taskArry(5,0)&" order by "&taskArry(6,0)&" "&taskArry(11,0)
'response.write(from_sql)
set rs=server.createobject("adodb.recordset")
rs.open from_sql,db_conn,3,1
if not rs.eof then
	from_rows=rs.GetRows()
else
	response.write("源数据库中没有记录!")
	response.End()
end if
rs.close
set rs=nothing
totalNum=ubound(from_rows,2)+1
response.Write("共有:"&totalNum&"条记录<br><p align=""center"">")
'导入数据
for i=0 to ubound(from_rows,2)	
	set rs=server.createobject("adodb.recordset")
	rs.open "select * from news where 1=0",conn,3,3
	rs.addnew
	for j=0 to ubound(target_field_arr)
		rs(target_field_arr(j))=from_rows(j,i)		
	next
	rs("classid")=taskArry(7,0)
	rs("templetid")=taskArry(8,0)
	rs("checked")=taskArry(9,0)
	rs("pagepath")=catalogSaveDir&"/html/"&date&"/ga"&id&i&".htm"
	rs("linkurl")=catalogDomainUrl&"/html/"&date&"/ga"&id&i&".htm"
	rs("moditime")=current_time
	rs.update
	rs.close
	set rs=nothing
	offset=offset+1
	percentNum=Cint((offset*100)/totalNum)
	if(percentNum=0) then
		percentNum=1
	end if
	response.write("成功采集第"&(i+1)&"条数据<br>"&Chr(10))
	response.flush()
	if(Cint(taskArry(12,0))<>0 and i>Cint(taskArry(12,0))) then
		exit for
	end if
next
response.write("</p><p><font color=""red"">采集完成!</font>")
response.write("用时："&CStr(FormatNumber((Timer-BeginTime)*1000,2))& "毫秒</p>")
%>

</body>
</html>
<%
db_conn.close
set db_conn=nothing
call connclose()
%>
