<%
'防止编码出错
gb2312_utf8
sub gb2312_utf8
	on error resume next
	err.clear
	Response.CharSet = "gb2312"'在netbox下出错
	Response.CodePage="936"
	Session.CodePage="936"'防止从其它编码转过来时出现乱码
	Response.ContentType = "text/html"
	if err<>0 then
		Response.Addheader "Content-Type","text/html; charset=gb2312"'在netBox下用个。
		'当调用时传递的参数多于一个时，方法1：call在前面，后加括号，方法2：不加call，不加括号。
	end if
end sub

'以前使用都是使用isNumeric来判断一个变量的值是不是数字，可是近来在使用中发现isnumeric并不好用：  
'举例：  
'"123"  
'"123.1"  
'"123,,,,111.222,,,333"  
'"123，，，1.22，，，3"  
'"123e+9"  
'"123d-8"  
'以上返回的都是True  

function sql_id(s1) '过滤数值型参数
	if s1&""="" then
		sql_id=""
		exit function
	end if
	
	if not isnumeric(s1) then 
		response.write(s1&"<br>ID只能为正整数！")
		response.end
	else

			sql_id=s1

	end if
end function

function sql_num(s1)' 过滤货币型参数
	if not isnumeric(s1&"") then 
		sql_num=0
	else
		sql_num=s1-0
	end if
end function

function sql_str(s1) '过滤字符型参数
	on error resume next
	if s1&""="" then
		sql_str=""
		exit function
	end if
	
	if instr(s1,"'") then
		s1=replace(s1,chrW(39),"&#39;")
	end if
	if instr(s1,chrW(34)) then
		s1=replace(s1,chrW(34), "&quot;")
	end if
	sql_str=s1
end function

function sql_kill(s1)' 过滤所有危险字符，适合传回来的列名
	If s1&"" = "" then
		sql_kill=""
	else
		s1=replace(s1,">","")
		s1=replace(s1,"<","")
		s1=replace(s1,"=","")
		s1=replace(s1,"(","")
		s1=replace(s1,")","")
		s1=replace(s1,",","")
		s1=replace(s1,";","")
		s1=replace(s1,"%","")
		s1=replace(s1,"*","")
		s1=replace(s1,"+","")
		s1=replace(s1,"-","")
		s1=replace(s1,chrW(9),"")
		s1=replace(s1,chrW(10),"")
		s1=replace(s1,chrW(13),"")
		s1=replace(s1,chrW(32),"")'空格
		s1=replace(s1,chrW(34),"")
		s1=replace(s1,chrW(39),"")
		sql_kill =s1
	end if
end function 
%>
<%
if session("公司简称")="" or session("userName")="" then'不能随意运行
	call get_cookie
end if
'response.Write "<br>" &request.Cookies("cur_userName")
'response.Write "<br>" & request.Cookies("cur_passWord")
'response.Write "<br>" & request.Cookies("cur_bm")
'response.End()
sub get_cookie
	on error resume next
	call get_cookies
end sub
conn=""'不能少，软定义
openDB

sub openDB()
	err.clear
	if session("公司简称")="" then
		response.write "<a href=../index2.asp?t=网络超时，请登陆。><br><p align=center>c连接超时，请重新登陆！</p></a>"
		response.end()
	end if
	bm=sql_kill(session("公司简称"))'编码
	db_path=""
%>	
	<%



	'on error resume next
	err.clear
	dim rs,sql_info_conn,sql_info_str
	set sql_info_conn=server.createobject("adodb.connection")
	'sql_info_str="driver={microsoft access driver (*.mdb)};DBQ="+server.mappath(db_path&"admin/db/global.asa")
	sql_info_str="driver={microsoft access driver (*.mdb)};DBQ="+get_db_path(db_path&"admin","db")&"\global.asa"
	'response.Write(sql_info_str)
	'response.End()
	sql_info_conn.open sql_info_str
	set rs=server.CreateObject("adodb.recordset")
	rs.open "select sql_user,sql_pass,sql_ip,sql_dbName,db_type from 公司名单 where 公司简称='"&bm&"'",sql_info_conn,1,1

	if err<>0 then
		response.write("错误原因: ")
		response.write(err.Description & "<br />")
		response.End()
	end if
	err.clear
	session("db_type")=rs("db_type")
	
    If ucase(rs("db_type")) = "SQL" Then'数据库类型
        strsql = "Provider = Sqloledb; User ID ="&rs("sql_user")&"; Password=" & rs("sql_pass")&"; Initial Catalog="&rs("sql_dbName")& "; Data Source="&rs("sql_ip")&";"
		rs.close
		set rs=nothing
		sql_info_conn.close
		set sql_info_conn=nothing
	else'ac数据库
		'db_path=server.mappath(db_path&"Data/"&bm&"/global.asa")
		db_path=get_db_path(db_path&"data",bm)&"\global.asa"
		strsql="driver={microsoft access driver (*.mdb)};DBQ="+db_path'速度快,不容易出错
		if set_col_default="y" then'需要设置列的默认值，连接ac数据库的代码不一样。
			strsql="Provider = Microsoft.Jet.OLEDB.4.0;Data Source="&db_path'支持default，但有时候会出问题。
		end if
	end if
	set conn=server.createobject("adodb.connection")
	'response.Write(strsql)
	'response.End()
	
	conn.open strsql
	if err<>0 then

			response.write("原因: ")
			response.write(err.Description & "<br />")
		
		'response.Write(strsql&"<br>")
		response.Write("连接数据库失败！您可以试着<a href=../index.asp target=_parent>重新登陆</a>，如果问题依然存在，请联系管理员。")
		response.End()
	end if
%>



<%
end sub
%>

<%

function get_db_path(path,folder_old)'现在有的路径 ，以前老的数据库名
	on error resume next
	dim fso,folder_s,folder_a,db_forder
	set fso=server.createobject("scripting.filesystemobject")

	set folder_s=fso.getfolder(server.MapPath(path))
	dim arr
	for each folder_a in folder_s.subfolders
		arr=split(folder_a,"\")
		folder_a_1=arr(ubound(arr))
		if left(folder_a_1,len(folder_old)+2)=folder_old&"__" then'左边几位相同,如db__swekldshrellsd,这里的+2是因为:db目录用db加双下划线加密码分开的。
			db_folder=folder_a_1
			exit for
		end if
	'	response.Write("<br>a:")&folder_a_1
	'	response.Write("<br>old:")&folder_old
	next
	'response.End()
	if db_folder="" then'说明还没有这文件夹,那就把以前的文件夹改名
	
		dim folder_new,path1,path2
		folder_new=folder_old&"__"&GetKey(32)
		
		path1=server.mappath(path&"/"&folder_old)
		path2=server.mappath(path&"/"&folder_new)

		if fso.folderexists(path1) then 
			fso.movefolder path1,path2
		else'以前的文件夹不存在,就直接新建一个新的文件夹如：yinteda__sldl
			fso.CreateFolder(path2)
		end if
		get_db_path=path2
	else '已经有这个文件夹了
		get_db_path=server.MapPath(path&"/"&db_folder)
	end if
end function
    
Function   GetKey(digits)    '参数digits，表示随机数的位数  
	dim   char_array(36)   
	dim   output,num   
	char_array(0)   =   "0"   
	char_array(1)   =   "1"   
	char_array(2)   =   "2"   
	char_array(3)   =   "3"   
	char_array(4)   =   "4"   
	char_array(5)   =   "5"   
	char_array(6)   =   "6"   
	char_array(7)   =   "7"   
	char_array(8)   =   "8"   
	char_array(9)   =   "9"   
	char_array(10)   =   "a"   
	char_array(11)   =   "s"   
	char_array(12)   =   "d"   
	char_array(13)   =   "f"   
	char_array(14)   =   "e"   
	char_array(15)   =   "f"   
	char_array(16)   =   "g"   
	char_array(17)   =   "h"   
	char_array(18)   =   "i"   
	char_array(19)   =   "j"   
	char_array(20)   =   "k"   
	char_array(21)   =   "l"   
	char_array(22)   =   "m"   
	char_array(23)   =   "n"   
	char_array(24)   =   "o"   
	char_array(25)   =   "p"   
	char_array(26)   =   "q"   
	char_array(27)   =   "r"   
	char_array(28)   =   "s"   
	char_array(29)   =   "t"   
	char_array(30)   =   "u"   
	char_array(31)   =   "v"   
	char_array(32)   =   "w"   
	char_array(33)   =   "x"   
	char_array(34)   =   "y"   
	char_array(35)   =   "z"   
	
	randomize   
	
	do   while   len(output)   <   digits   
	num   =   char_array(Int((35)   *   Rnd   +   0))   
	output   =   output   +   num   
	loop   
	
	GetKey   =   output   
End   Function
%>
<%
	dim tbName
	fileName=sql_str(request("fileName"))
	tbName=conn.execute("select tbName from purview where fileName='"&fileName&"'")(0)
	response.Write(tbName)
%>