When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs



















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

.Net Developer
Professional Technical Resources
US-WA-Bellingham

Justtechjobs.com Post A Job | Post A Resume

Published: Monday, January 17, 2000

Creating a SQL Query Test Page
By Geno Timlin


When creating a data-driven web site, you obviously need to use a number of SQL queries to grab datasets from your database. Before you use a particular SQL query in one of your production ASP pages, though, you want to ensure that it returns the correct information. Therefore, a SQL query test web page, one that allows you to try various SQL statements and view their results, will often come in handy.

- continued -

For every web database project I work on, I pop this code somewhere onto the server. This page allows me to test out SQL queries before I put them into my ASP. Recently I was doing a project on a co-located SQL Server in Atlanta. I used this page to set up all the tables from my home office in Florida.

The idea is simple. You put SQL code into the form, select the DSN from the drop down list and click submit. If the recordset contains columns, a table is built to display all rows and all columns. If the SQL is an data manipulation statement, a message appears stating that the command completed successfully.

One of the cool features is the clipboard. You can cut and paste SQL into the clipboard and it will be there each time you submit the form. The drop down list of DSN's is fairly simple, you set up an array of DSN strings and loop through them to fill the drop down list. Sometimes I use another textarea for the DSN instead of the drop down list. This allows me to try out different connection strings.

I've become familiar enough with the Microsoft error messages so I haven't built in any fancy error checking.

Well, here's the code, I hope you find it useful! If you have any questions, comments, or ideas for improvement, please email me.

<% Response.Buffer=True %>
<%
  'database startup code
  dim RS	'recordset object
  Set RS = Server.CreateObject("ADODB.Recordset")

  'data source strings for drop down list
  dim dsnarray(2)
  
  'be sure to only populate the array elements zero
  'through the upper bound of the DSN array (2 in this example)
  dsnarray(0) = "MP3"
  dsnarray(1) = "Protfolio"
  dsnarray(2) = "ProductsDB"

  'retrieve the form values
  sql = Request.Form("sql")	'the SQL statement
  clp = Request.Form("clp")	'the clipboard
  dsn = Request.Form("dsn")	'the data source string
%>
<html>
<head>
  <title>SQL Test</title>
<style>
	TD {font-size: smaller }
</style>
</head>
<body bgcolor="#cecece">

<form action="sqltest.asp" method=POST>
<table border=0 cellspacing=0>
<tr>
  <td>
    <b>SQL Code</b>
  </td>
  <td>
    <b>Clipboard</b>
  </td>
</tr>

<tr>
  <td>
    <textarea name="sql" rows="8" cols="50" wrap=soft><%=sql%></textarea>
  </td>
  <td>
    <textarea name="clp" rows="8" cols="30" wrap=soft><%=clp%></textarea>
  </td>
</tr>

<tr>
  <td colspan=2>
    <!-- using the drop down list of connect string -->
	<select name="dsn">
	<% ' loop through the array of DSN's select the current one 
	  for i = LBound(dsnarray) to UBound(dsnarray)
		if dsnarray(i) = dsn then
	  	  Response.Write("<option selected>" & dsnarray(i))
		else
		  Response.Write("<option>" & dsnarray(i))
		end if
	  next
	%>
	</select>
  </td>
</tr>

<!-- this code allows you to test different connect strings -->
<!--
<tr>
  <td colspan=2>
    <textarea name="dsn" rows="2" cols="80" wrap=soft><%=dsn%></textarea>
  </td>
</tr> -->

</table>

  <input type=submit>
</form>

<% 
  'for long winded queries, this will write out the response buffers 
  Response.Flush
%>

<% 
if sql <> "" then	' execute the SQL if it's not empty
  RS.Open sql, dsn
  Response.Write("<table border=1 cellspacing=0>")
  if RS.State = 1 then	'if the recordset has rows		
 	'show the column names
	Response.Write("<tr bgcolor=LightSteelBlue>")

	for each f in RS.Fields
      Response.Write("<td><b>" & f.Name & "</b></td>")
	next

	Response.Write("</tr>")
		
	'show the rows
	do while not RS.EOF
	  Response.Write("<tr bgcolor=White>")

	  for each f in RS.Fields
	     Response.Write("<td>" & f.Value & "</td>")
	  next
	
	  Response.Write("</tr>")

	  RS.MoveNext
	loop
  else
	'DML was performed
	Response.Write("<tr bgcolor=White><td><b>")
	Response.Write("Command Completed Successfully</b>")
	Response.Write("</td></tr>")
  end if

  Response.Write("</table>")
end if 
%>

<%	
  'database clean up code
  Set RS = Nothing 
%>
</body>
</html>

Notice that I have the list of System DSN's hard coded into the above code. The disadvantage with that is when new DSNs are added, or old ones are removed, this ASP page must be updated. To overcome this problem, I highly recommend that you use Mike Shaffer's free DSN listing component. The complete source code and an detailed article can be found here.

Here is a screen shot of the system in action!

The SQL Query Test Page in action!

Happy Programming!


Attachments:

  • Download the source code in text format


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article

  • internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info

    Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Whitepapers and eBooks

    Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
    IBM Solutions Brief: Go Green With IBM System xTM And Intel
    HP eBook: Simplifying SQL Server Management
    IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
    Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
    Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
    Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
    Intel PDF: Quad-Core Impacts More Than the Data Center
    Intel PDF: Virtualization Delivers Data Center Efficiency
    Go Parallel Article: PDC 2008 in Review
    Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
    Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
    Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
      PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
    Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
    Go Parallel Article: Q&A with a TBB Junkie
    IBM Whitepaper: Innovative Collaboration to Advance Your Business
    Internet.com eBook: Real Life Rails
    IBM eBook: The Pros and Cons of Outsourcing
    Internet.com eBook: Best Practices for Developing a Web Site
    IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
    Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
    IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
    Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
    HP eBook: Guide to Storage Networking
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES