asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Code Samples
Components
Tools
New
Free
Downloads
Vendors
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

One of the main problems I've always faced was writing code to manipulate database entities using SQL. You can write it in the source itself and concatenate large strings in order to issue the statement through ADO. This works just fine so long as you never need to change you database.

However, I've worked on a number of projects that need to work on both Oracle and SQLServer, or even DB2 on an MVS mainframe. In order to accomodate these changes, I created a simple class that treats SQL like a parameterized string. All the programmer needs to do is create either a resource file for statically linked code, or a text file for dynamically linked code. The class opens either the named resource or the text file and reads its contents to create a list of tokens. These tokens can be assigned values. The progam can then ask the object to retrieve the formatted SQL statement and execute it. The text below represents a formatted SQL template that the class can read and then format based on provided parameters.


UPDATE MY_TABLE
SET
	MY_FIELD = {MyField:C(11)},
	MY_NUMBER = {MyNumber:N},
	My_DATE={MyDate:D}
WHERE
	MY_ID={MyID:N};

The source code would then read:



Dim metaData
Set metaData = Server.CreateObject("MetaSql.CSqlMetaData")

Call metaData.loadFile("myTestSql.sql")
metaData.param("MyField") = "This isn't a bad test of the class's automated
string formatting."
metaData.param("MyNumber") = 10
metaData.param("MyDate") = Now()
metaData.param("MyID") = Request("my_id")

Call adoConnection.Execute(metaData.sqlSource)

The preceding code would create the following SQL statement:


UPDATE MY_TABLE
SET
	MY_FIELD = 'This isn''t',
	MY_NUMBER = 10,
	My_DATE='12/28/2000'
WHERE
	MY_ID={MyID:N};

This doesn't look like much unless you need to convert the SQL to Access or Oracle which both have their own ideas about formatting dates, etc. Even more important, the code logic above doesn't need to change at all if the SQL changes. You can replace the UPDATE statement with a stored procedure. All the logic needs to know is that there is a named file that contains SQL expecting a certain number of parameters. You could also use the following logic for updating tables based on <form> input.


Dim metaData
Dim tokenList
Set metaData = Server.CreateObject("MetaSql.CSqlMetaData")

Call metaData.loadFile("myTestSql.sql")
tokenList = metaData.paramList

If IsArray(tokenList) Then
	For i = LBound(tokenList) To UBound(tokenList) Step 1
		metaData.param(tokenList(i)) = Request(tokenList(i))
	Next

	Call adoConnection.Execute(metaData.sqlSource)
End If

In the code mentioned above, all the developer needs to do is create a syntactically correct SQL statement, and then create correspondingly named input fields. The object even formats the data on the way in to help reduce errors from user input. Obviously some forms will require additional type checking, but the bulk of the work of creating the SQL statement and extracting the supplied data has been done for you.

Download the supplementary zip file, which contains the COM object, some sample code for your Web site, a sample SQL template, and a readme.txt file explaining how it all fits together.

Submitted by Robert J. Morris



email this code sample to a colleague

Related Articles
Dec 10, 1996 - ODBC 3.0 Connection Pooling
This issue of 15 Seconds contain an example of how to create an ISAPI server extension in MSVC 4.2 with ODBC 3.0 connection pooling. There is also an evaluation of ODBC 3.0, OLEDB, ADO and DAO.
[Read This Article]  [Top]
May 31, 1997 - Connection Pooling with ASP
Connection pooling might be the easiest way to speed up your dynamic web pages reading from SQL Server. Unfortunately, connection pooling within is turned off by default in Active Server pages. Probably because connection pooling is rarely understood in its entirety. This issue discusses connection pooling with ASP, ISAPI, IDC, and Visual Basic applications. Included is a discussion about ODBC 3.0 and the newest bug fix for ODBC.
[Read This Article]  [Top]
Apr 15, 1998 - File Repository Weblication using IIS 4.0
In this article Amos El-Roy demonstrates how to create a file repository using ASP pages. A seamless approach that maximizes accessibility and lowers administrative overhead is illustrated in the article's example, which is available for download.
[Read This Article]  [Top]
Oct 21, 1999 - Updating Excel From the Web
Bill Jeffries's article on Excel's Web Query tool demonstrates how to update selected spreadsheet cells instantly over an HTTP connection.
[Read This Article]  [Top]
Nov 11, 1999 - Database-Driven WWW Help System
The help system presented in Vujosevic and Laberge's article is self contained and can be updated and altered without impacting the original Web application. Much like an online book, the help icon in the Web application dives into an application system for the help option. Each Web page has its own separate help page with a database that contains one row in a table for every calling Web page. Sample code is provided.
[Read This Article]  [Top]
Related Books
Working with Active Server Pages
Teach yourself Active Web Database Programming in 21 Days
Related Knowledge Base Articles
INFO: ODBC Connection Pooling and ADO
Q166886 - 1997.06.23
FIX: Repetitive Connect/Disconnect May Shut Dow...
Q164734 - 1997.06.23
INF: SQL Logs 17832 With Multiple TCP\IP Connec...
Q154628 - 1997.06.23
INF: How to Access SQL Server Within Active Ser...
Q169377 - 1997.06.23
INF: Frequently Asked Questions About ODBC Conn...
Q169470 - 1997.07.10
HOWTO: Redistributing ADO 1.0 or ADO/R 1.0 with...
Q168122 - 1997.08.09
HOWTO: Calling SQL Server Stored Procedures fro...
Q164485 - 1997.08.21
HOWTO: Determining How to Define a Stored Proce...
Q165156 - 1997.08.21
PRB: Accessing SQL Database Fails on Second Att...
Q166659 - 1997.09.06
INFO: Underlying OLE and OLEDB Provider Errors ...
Q168354 - 1997.09.23



Support the Active Server Industry

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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