数据库的Query Analyzer
2001年7月6日 10:45:33·徐长友·http://yousoft.home.chinaren.com
很多人一定用过Query Analyzer,这个工具的功能如何如何,就不用我说了,这次给大家介绍下Web下的Query Analyzer,界面如图1,使用ASP写的。
(图1)
源程序如下:
<%
dim conn
dim connstr
on error resume next
if request("selectdb")="mdb" then
if request("dbname")<>"" then
connstr="DBQ="+server.mappath(request("dbname"))+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
set conn=server.createobject("ADODB.CONNECTION")
if err.number<>0 then
err.clear
set conn=nothing
response.write "错误:数据库连接出错!"
else
if request("dbpass")<>"" then
conn.open connstr
else
Conn.Open connstr,"admin",request("dbpass")
end if
if err then
set conn=nothing
response.write "错误:数据库连接出错!!!<br>" + err.description
err.clear
end if
end if
end if
elseif request("selectdb")="mssql" then
if request("dbname")<>"" then
connstr = "Provider=SQLOLEDB.1"
connstr = connstr & ";Data Source=" & request("sqlip") 'sql_server数据库
connstr = connstr & ";User ID=" & request("sqladmin") '数据库服务器用户
connstr = connstr & ";Password=" & request("sqlpass") '登录口令
connstr = connstr & ";Initial Catalog=" & request("sqldb") '数据库名
set conn=server.createobject("ADODB.CONNECTION")
if err.number<>0 then
err.clear
set conn=nothing
response.write "错误:数据库连接出错!"
else
conn.open connstr
if err then
set conn=nothing
response.write "错误:数据库连接出错!!!<br>" + err.description
err.clear
end if
end if
end if
end if
sub endConnection()
conn.close
set conn=nothing
end sub
%>
<HTML>
<HEAD>
<TITLE>Query Analyzer</TITLE>
<STYLE type=text/css>BODY {FONT-FAMILY: "宋体", "Arial Narrow", "Times New Roman"; FONT-SIZE: 9pt}
P {FONT-FAMILY: "宋体", "Arial Narrow", "Times New Roman"; FONT-SIZE: 9pt}
BR {FONT-FAMILY: "宋体", "Arial Narrow", "Times New Roman"; FONT-SIZE: 9pt}
TD {FONT-FAMILY: "宋体", "Arial Narrow", "Times New Roman"; FONT-SIZE: 9pt}
.p9 {FONT-SIZE: 9pt; LINE-HEIGHT: 14pt}
A:link {COLOR: #004080; FONT-SIZE: 9pt; LINE-HEIGHT: 14pt; TEXT-DECORATION: none}
A:visited {COLOR: #004080; FONT-SIZE: 9pt; LINE-HEIGHT: 14pt; TEXT-DECORATION: none}
A:hover {COLOR: #ff0000; FONT-SIZE: 9pt; LINE-HEIGHT: 14pt; TEXT-DECORATION: underline}
.p105 {FONT-SIZE: 10.5pt}
INPUT.yellowbtn {BACKGROUND-COLOR: #88a3f2; COLOR: #000000; FONT-SIZE: 9pt}
</STYLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" topmargin="0">
<script language=JavaScript>
function setWB(x) {
document.main.whichbutton.value = x;
document.main.submit();
}
function showSQL(x) {
sqlvalue = document.main.sql.value;
if (x == 0) {
smsg = "\n怎样使用sql语句帮助:\n\n";
smsg = smsg + "1. 在选择框里选一个命令\n";
smsg = smsg + "2. 点击[显示]按钮\n";
smsg = smsg + "3. sql语句会显示在左边的文本框里面\n\n";
smsg = smsg + "4. 修改此语句来适合你数据库结构\n\n";
smsg = smsg + "5. 根据语句查询结果或执行命令\n\n";
alert(smsg); }
else if (x == 1) {
smsg = "SELECT columnName1, columnName2\n";
smsg = smsg + " FROM tableName\n";
smsg = smsg + " WHERE columnName = 'value'\n\n";
document.main.sql.value = smsg;
}
else if (x == 2) {
smsg = "INSERT INTO tableName\n";
smsg = smsg + " (columnName, IntegerColumnName)\n";
smsg = smsg + " VALUES ('value', numericValue)\n\n";
document.main.sql.value = smsg;
}
else if (x == 3) {
smsg = "UPDATE tableName\n";
smsg = smsg + " SET columnName = 'value'\n";
smsg = smsg + " WHERE columnName = 'value'\n\n";
document.main.sql.value = smsg;
}
else if (x == 4) {
smsg = "DELETE columnName\n";
smsg = smsg + " FROM tableName\n";
smsg = smsg + " WHERE columnName = 'value'\n\n";
document.main.sql.value = smsg;
}
else if (x == 5) {
smsg = "CREATE TABLE tableName\n";
smsg = smsg + " (columnName1 varchar(20),\n";
smsg = smsg + " columnName2 char(20),\n";
smsg = smsg + " columnName3 integer)\n\n";
document.main.sql.value = smsg;
}
else if (x == 6) {
smsg = "DROP TABLE tableName\n\n";
document.main.sql.value = smsg;
}
else if (x == 7) {
smsg = "SELECT a.columnName, b.columnName\n";
smsg = smsg + " FROM tableName a, tableName b\n";
smsg = smsg + " WHERE a.columnName = b.columnName\n\n";
document.main.sql.value = smsg;
}
}
function helpme(){
helpmsg="在上面的那个文本框输入一句sql语句,例如:\n\n";
helpmsg+="select * from tablename\n\n";
helpmsg+="如果需要查询结果,则按[查询结果],如果只\n";
helpmsg+="需执行一句sql语句,则按[执行sql语句],一\n";
helpmsg+="般select是用于查询的,update、delete、\n";
helpmsg+="create table等是用于执行的。\n\n";
helpmsg+="在sql语句帮助表里可以获得一些基本sql语句\n";
helpmsg+="的语法。";
alert(helpmsg);}
</script>
<form action="<%=request.servervariables("script_name")%>" method=post name=main>
<br>
<table border=0 cellpadding=2 cellspacing=2 width=100% align="center">
<tr bgcolor="#00CCFF">
<td width="157">
<input type="radio" name="selectdb" value="mdb" <%if request("selectdb")="mdb" then response.write "checked" %>>
MDB数据库</td>
<td width="588"> 数据库:
<input type="text" name="dbname" value="<%=request("dbname")%>">
(例如:db.mdb,或dir/db.mdb)<br>
密 码:
<input type="password" name="dbpass" value="<%=request("dbpass")%>">
</td>
</tr>
<tr bgcolor="#00FFCC">
<td height="40" width="157">
<input type="radio" name="selectdb" value="mssql" <%if request("selectdb")="mssql" then response.write "checked" %>>
MS_SQLServer</td>
<td height="40" width="588"> 服务器:
<input type="text" name="sqlip" value="<%=request("sqlip")%>">
(SQLSERVER 的IP地址)<br>
数据库:
<input type="text" name="sqldb" value="<%=request("sqldb")%>">
<br>
登录名:
<input type="text" name="sqladmin" value="<%=request("sqladmin")%>">
<br>
密 码:
<input type="password" name="sqlpass" value="<%=request("sqlpass")%>">
</td>
</tr>
<tr bgcolor="#33CCFF">
<td width="157" rowspan="2">
<table border=0 cellpadding=2 cellspacing=2
width="83%" align="center">
<tr>
<td align=center bgcolor=#00CCCC
valign=center>SQL向导</td>
</tr>
<tbody>
<tr>
<td align=left bgcolor=#00CCCC
valign=center>
<div align="center">
<select
name=sqlsyntax size=5>
<option selected>Choose SQL</option>
<option>Select</option>
<option>Insert</option>
<option>Update</option>
<option>Delete</option>
<option>Create Table</option>
<option>Drop Table</option>
<option>Simple Join</option>
</select>
</div>
</td>
</tr>
<tr>
<td align=left bgcolor=#00CCCC
valign=center>
<div align="center">
<input name=sqlasst1 onClick=showSQL(document.main.sqlsyntax.selectedIndex); type=button value="显示">
<input name=sqlasst2 onClick="document.main.sql.value='';" type=button value="清除">
</div>
</td>
</tr>
</table>
</td>
<td width="588"> SQL Query : </td>
</tr>
<tr>
<td width="588" align="center" bgcolor="#33CCFF">
<textarea cols=50 name=sql rows=12 wrap=VIRTUAL></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input name=whichbutton type=hidden value="NORS">
<input name=action2 type=hidden value=exec>
<input name=selindex type=hidden>
<b>
<input name=b1 class=yellowbtn onClick="setWB('GetRS');" type=button value="查询结果">
<input name=b2 class=yellowbtn onClick="setWB('NORS');" type=button value="执行SQL语句">
<input name=b3 class=yellowbtn onClick="helpme();" type="button" value="帮助">
</b> <b>
<script language=JavaScript>
document.main.sql.focus();
if (document.main.selindex.value != "") { document.main.db.options[document.main.selindex.value].selected = true; }
</script>
</b></td>
</tr>
</table>
<br>
</form>
<p>
<%
dim sql,rs
ifrs=request("whichbutton")
sql=request("sql")
if sql<>"" then
select case ifrs
case "NORS"
conn.execute sql
if err then
response.write "这句sql语句有错误,没有完全执行。<br>"&err.description&"<br>"&sql
else
response.write "执行成功!"
end if
case "GetRS"
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
if err then
response.write "这句查询sql语句有错误,没有完全执行。<br>"&err.description&"<br>"&sql
else
response.write "找到了<b>"&cstr(rs.recordcount)&"</b>个结果"
%>
<table border="1" align="center">
<tr>
<%
colnum=rs.fields.count
for i=0 to rs.fields.count-1 %>
<td bgcolor="#33CCFF"><font color='red'><%=rs(i).name%></font></td>
<%next%>
</tr>
<%do while not rs.eof
k=0 %>
<tr>
<%for k=0 to colnum-1%>
<td><%=rs(rs(k).name)%></td>
<%next %>
</tr>
<% rs.movenext
loop
%>
</table>
<%
rs.close
set rs=Nothing
end if
end select
end if
endconnection
%>
</BODY>
</HTML>
Query Analyzer就这么简单,当然还需要改进,这就看你的了,呵呵...:)