<!--#include file="fy.Function.asp"-->
<!--#include file="fycms.asp"-->
<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>数据库操作</title>
<link rel="stylesheet" type="text/css" href="css/base.css">
<link rel="stylesheet" type="text/css" href="css/content.css">
</head>
<body>
<div class="content"> 
	<div class="breadnav">当前位置：<a href="#">后台首页</a> >> 数据库维护</div>
<%
 If Not fycms.ChkAdmin() Then '超级管理员检测
	Call fycms.fycmsErr("")
 End If 
Dim ShowErr
Select Case Request.QueryString("Action")
 		Case "ExecSql"
			Call ExecuteSql()
		Case "Compress"
			Call CompactDatabase()
		Case Else
			Call Main()
End Select
Sub Main()
	 
 %>
    <div class="opers">
   	<a href="?" class="btn">返回首页</a>
    	<a href="?Action=Compress" class="btn btn_primary">数据库压缩</a>
     </div>
 <div class="divtable"> <dl>
            <dt>数据库维护:</dt>
            <dd>
                说明：注：一次只能执行一条Sql语句。如果你对SQL不熟悉，请尽量不要使用。否则一旦出错，将是致命的。<br>
        建议使用查询语句.如：Select top 10 Title From Article order by ID desc,尽量不要使用delete,update等命令
            </dd>
         </dl>
 </div>
 <form name="ExecuteForm" method="post" Action="fy.Data.asp?Action=ExecSql&Flag=Result" onSubmit="return CheckForm()">
    <div class="divtable"> 
        
        
        <dl>
            <dd>
               <textarea name="Sql" rows="10" wrap="OFF" cols="100">Select top 10 Title From Article order by ID desc</textarea>
             </dd>
         </dl>
      
            <dl>
             <dd>
                <input type="submit" name="submit1" class="btn btn_primary" value="立即执行">
            </dd>
         </dl>
        
    </div>
    </form>

	

<%  
End Sub  
	Sub ExecuteSQL()

	  Dim SelectSQLTF,ExecSQLErrorTF,ExeResultNum,ExeResult,FiledObj
		Dim Sql:Sql =request("Sql")
	    if SQL="" Then Exit Sub
		If Instr(1,lcase(Sql),"delete from log")<>0 then
			echo "error"
				Exit Sub
		End If
	    SelectSQLTF = (LCase(Left(Trim(Sql),6)) = "select")
		Conn.Errors.Clear
	 '	On Error Resume Next
		if SelectSQLTF = True then
			  Set ExeResult = Conn.Execute(Sql,ExeResultNum)
		else
			  Conn.Execute Sql,ExeResultNum
		end if
         
		If Conn.Errors.Count<>0 Then
			  ExecSQLErrorTF = True
			  Set ExeResult = Conn.Errors
		Else
			  ExecSQLErrorTF = False
		End If
		if ExecSQLErrorTF = True then
		%>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
		 <thead> <tr> 
			<th> 
			  错误号</th>
			<th> 
			  来源</th>
			<th> 
			  描述</th>
			<th> 
			  帮助</th>
			<th> 
			  帮助文档</th>
		  </tr>
		  <tr height="20"> 
			<td nowrap> 
			  <% = Err.Number %> </td>
			<td nowrap> 
			  <% = Err.Description %> </td>
			<td nowrap> 
			  <% = Err.Source %> </td>
			<td nowrap> 
			  <% = Err.Helpcontext %> </td>
			<td nowrap> 
			  <% = Err.HelpFile %> </td>
		  </tr></thead>
		</table>
		<%
		else
		%>	
    <div class="opers">
    	<a href="?" class="btn">返回首页</a>
    	<a href="?Action=Compress" class="btn btn_primary">数据库压缩</a>
    </div>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
		  <%
			if SelectSQLTF = True then
		%>
		  <tr>
		<%
				For Each FiledObj In ExeResult.Fields
		%> <th><% = FiledObj.name %></th>
		 
		<%
				next
		%>
		  </tr>
		<%
				do while Not ExeResult.Eof
		%>
		  <tr>
		<%
					For Each FiledObj In ExeResult.Fields
		%>
			<td> 
			 
				<%
				 if IsNull(FiledObj.value) then
					echo("&nbsp;")
				 else
					echo(server.HTMLEncode(FiledObj.value))
				 end if
				 %>
			 </td>
		<%
					next
		%>
		  </tr>
		<%
					ExeResult.MoveNext
				loop
			else
		%>
		  <tr>
			<td  height="26">
		<div align="center">执行结果</div></td>
		  </tr>
		  <tr>
			<td height="20"  class="td_bg">
		<div align="center">
				<% = ExeResultNum & "条纪录被影响"%>
			  </div></td>
		  </tr>
		<%
			end if
		%>
		</table>
		<%
		  end if
		 End Sub
 
 
 Public Function CompactDatabase()
				 dim dbpath
				dbpath = MyDbPath&db
				On Error Resume Next
				Dim strTempFile, fso, jro, ver, strCon, strTo, LCID
				Set fso = Server.CreateObject("Scripting.FileSystemObject")
				strTempFile = DBPath
				strTempFile = Left(strTempFile, InStrRev(strTempFile, "\")) & fso.GetTempName
				Set jro = Server.CreateObject("JRO.JetEngine")
				LCID = Conn.Properties("Locale Identifier").Value
				CloseConn
				strTo = "Provider=Microsoft.Jet.OLEDB.4.0; Locale Identifier=" & LCID & "; Data Source=" & Server.MapPath(strTempFile) & "; Jet OLEDB:Engine Type=" & ver
				jro.CompactDatabase ConnStr, strTo
				CompactDatabase = False
				If Err Then
					fso.DeleteFile Server.MapPath(strTempFile)
				Else
					fso.DeleteFile Server.MapPath(DBPath)
					fso.MoveFile Server.MapPath(strTempFile), Server.MapPath(DBPath)
					If Err Then
						fso.DeleteFile Server.MapPath(strTempFile)
					Else
						CompactDatabase = True
					End If
				End If
				Set jro = Nothing
				Set fso = Nothing
				'重新打开数据库
				ConnectionDatabase()
			   if  CompactDatabase=true then
					ShowErr = "数据库压缩和修复成功"
			   else
				 ShowErr = "操作失败"
			   end if
					Call fycms.ActErr(ShowErr,"","")
					Response.end
		End Function


Function CheckDir(FolderPath)
dim fso1
	folderpath=Server.MapPath(".")&"\"&folderpath
    Set fso1 = CreateObject("Scripting.FileSystemObject")
    If fso1.FolderExists(FolderPath) then
       '存在
       CheckDir = True
    Else
       '不存在
       CheckDir = False
    End if
    Set fso1 = nothing
End Function

Function MakeNewsDir(foldername)
	dim f,fso1
	 MakeNewsDir = False
	   Set fso1 = CreateObject("Scripting.FileSystemObject")
        Set f = fso1.CreateFolder(foldername)
        MakeNewsDir = True
    Set fso1 = nothing
End Function
%>
	<script language="javascript">
	<!--
	 function CheckForm()
	 {if (document.ExecuteForm.Sql.value=='')
	  {
	  alert('请输入SQL查询语句！');
	  document.ExecuteForm.Sql.focus();
	  return false;
	  }
	  ExecuteSQLFrame.location.href="fy.Data.asp?Action=ExecSql&Flag=Result&SQL="+document.ExecuteForm.Sql.value;
	  return false;
	  }
	-->
	</script>

</div>
</body>
</html>
