做过access网站的相信会有一个烦恼,数据库托管在别人的服务器,要操作比较麻烦,想查查数据是否异常也不方便,于是简单地实现一个"查询分析器",方便操作.
<!--#include file="Include/fConnStr.asp"-->
<!--上面是连接数据库,不多说-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>SQL</title>
</head>
<%
Select Case Request("btnLevel")
Case "Main"
Case Else
Call zMain()
End Select
%>
<body>
<%
'==============================
'主画面
'==============================
Sub zMain()
Dim strSQL,strCode,Column,RsCount
Dim intNum
RsCount=0
strSQL =Trim(Request.Form("SQL"))
strCode =Trim(Request.Form("Code"))
%>
<table cellspacing="0" cellpadding="0" width="100%" border="1" class="tableBorder" style="border-collapse: collapse">
<form action="" method="post">
<tr>
<td>
<textarea name="SQL" rows="15" cols="150"><%=strSQL%></textarea><br />
<input type="text" name="Code" />[*如要更新数据,请输入]<br />
<input type="submit" value="提交" />
</td>
</tr>
</form>
</table>
<%
If strSQL <> "" Then
If LCase(Left(strSQL,6))="select" Then
Set rstMain = Server.CreateObject("ADODB.Recordset")
rstMain.Open strSQL,fGetCnnStr,1,1
Column =rstMain.Fields.Count
RsCount =rstMain.RecordCount
Else
If LCase(strCode)="execute" Then '这里设置你的"密码",如要做SELECT以外的操作,则需要输入,防止误操作
fGetCnnStr.execute(strSQL)
End If
End If
End If
%>
<%
If RsCount>0 Then
%>
<table cellspacing="0" cellpadding="0" width="100%" border="1" class="tableBorder" style="border-collapse: collapse">
<tr align="center">
<%For intNum=0 To Column-1%>
<th><%=rstMain.Fields(intNum).Name%></th>
<%Next%>
</tr>
<%While Not rstMain.Eof%>
<tr align="center">
<%For intNum=0 To Column-1%>
<td><%=rstMain(intNum)%></td>
<%Next%>
</tr>
<%
rstMain.MoveNext
WEnd
%>
</table>
<%Else%>
<table cellspacing="0" cellpadding="0" width="100%" border="1" class="tableBorder" style="border-collapse: collapse">
<tr align="center">
<td>没有记录</td>
</tr>
</table>
<%
End If
%>
<%
End Sub
%>
</body>
</html>
做INS,UPD,DEL这些就简单了.查询方面:主要就用到以下两句:
Column =rstMain.Fields.Count '获取本句SQL一共有多少字段
rstMain.Fields(intNum).Name '这句是获取搜索的field名.
实现非常简单,但比较实用.