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
International

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]
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


Support the Active Server Industry

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