Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article
Stardeveloper RSS Feed
Hosted by Securewebs.com
 
Home : .NET : ADO : Running Stored Queries in Access Database
 
RSS - Read full length articles at Stardeveloper using Stardeveloper RSS Feed RSS

Running Stored Queries in Access Database

by Faisal Khan. Follow Faisal Khan on Twitter Follow Faisal Khan on Facebook

Overview

In this tutorial we are going to talk about yet another powerful feature of database programming, Stored Queries. Most of the time we use ASP pages, we use them to retrieve or manipulate records in the database. These database connections are resource heavy and every query takes time on part of the server. Thus we should try to make use of everything possible to optimize and speedup database access as much as possible. One of the ways is to retrieve records as arrays which I discussed in my article ' Speeding up Database Access' earlier. Now we are going to talk about Stored Procedures which provide another way of optimizing database access.

Stored Queries
They are actually SQL statements which are compiled and stored in the database. Normally we manually provide SQL statement every time we want to manipulate records in the database. e.g,

	Dim con
		Set con = Server.CreateObject("ADODB.Connection")

	' executing query
	con.Execute "insert into Table(col1, col2) values(val1, val2);"

	con.Close
	Set con = Nothing

In above code an Insert statement was executed. No matter how small and simple an SQL statement is, it takes times to be ported to the database server over the network where it is interpreted and then response generated.

Why use Stored Queries ?
Stored Queries are not textual SQL statements, rather they are compiled and stored in the database you are using just like Stored Procedures in SQL Server. They provide enormous speed improvements ( especially under stress ) as they neither need to be ported to the database server nor interpreted.

In an N-Tier environment they also help separate business layer from the data layer.

Example Code :
Code I provided earlier on this page has been edited to accomodate an Insert Stored Procedure in place of an Insert statement.

	Dim con
		Set con = Server.CreateObject("ADODB.Connection")

	' executing query
	con.Execute "exec InsertProc val1, val2"

	con.Close
	Set con = Nothing

Please notice that Stored Queries are equivalent to Stored Procedures in Microsoft SQL Server. The procedure of creating and running Stored Procedures in Microsoft SQL Server is different. In Access database, stored procedures are known as 'Queries'.

Access Database :
Start Microsoft Access and create a new blank database and save it as "StoredProc.mdb". Now create a new table in 'design view' and save this table as 'Names'. It contains only two fields and should look like following :

Names - Table
Names - Table

You should have 'Names' listed in your tables list now, as shown below :

Table List
Table List

Now click Queries under the Objects tab on the left. Now double click 'Create query in Design View'.

Create query in Design View
Create query in Design View

Two windows will open. Close the 'Show Tables' window by clicking the close button.

'Show Table' Window
"Show Table" Window

Now right click in the 'Query1 : Select Query' window and click 'SQL View' option as shown below.

Select Query
Select Query

You should get a wide white window with something like following written on it's top left.

Query1 : Select Query
Query1 : Select Query

Our first Stored Query
We are now ready to write down our first stored query. This will be a simple insert SQL statement which will require single value ( Name ) to be inserted into our Names table.

Now copy the following SQL statement and paste it into the 'Query1 : Select Query' window :

	INSERT INTO [Names] (Name) VALUES ([@newName]);
InsertProc
InsertProc

Explanation
Above is a simple Insert SQL statement like the one you use in ASP pages. The only difference is that we don't give it any value except a variable name ( @newName ) which will be our only argument to this stored query when we go to run it. This argument will be any value we want inserted in the database.

Now close the 'Query1 : Select Query' window. A small window will pop asking if you want to save changes to the design of query 'Query1'.

Save Changes Window
Save Changes Window

Click 'Yes'. A 'Save As' window will pop up asking you to give name to this Query. Give it InsertProc. Note this can be any name, I've given it this name so as to be a shortcut for 'Insert Procedure'. Many developers like appending 'sq_' before the stored query name e.g, in our case the stored procedure ( Query ) will become 'sq_InsertProc'. As far as I am concerned I don't care what name you give to it, just make sure it is easily rememberable, ;)

You should now have InsertProc Query listed under 'Queries' tab.

InsertProc Listed
InsertProc Listed

Now lets try this one out. We can do this even without running it in an ASP page ( we will but later ). Double click the listed InsertProc icon. A warning window will pop up ( see below ) telling you that you are about to modify data in your table, click 'Yes'.

Warning Window
Warning Window
Now you will see a small 'Enter Parameter Value' window with our variable @newName written over the input box. Enter any name and hit 'OK'.

Enter Parameter Value
Enter Parameter Value

Another warning window will open telling you that you are about to append one row, click 'Yes'.

Warning Window
Warning Window

Now to check if record has been entered or not go to 'Tables' window and double click 'Names' table. You should see your entered name in there.


 ( 2 Remaining ) Next

Comments/Questions ( Threads: 18, Comments: 30 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. connection
  2. Passing QueryString parameters to a select query
  3. rUNNING TWO STORED QUERYS ON ONE ASP PAGE -ERROR PLEASE HELP ( 1 Reply )
  4. How can I execute a Stored Query using C# ( 1 Reply )
  5. Help me! Big problem!
  6. Help me! Big problem!
  7. Using parameters in Stored Queries
  8. Better than Access stored query ...? ( 1 Reply )
  9. Return Value in Store Procedure ( 2 Replies )
  10. Pleas... help me
  11. keep getting an error ( 1 Reply )
  12. Awesome!!!
  13. Running stored queries
  14. Passing a veriable ( 1 Reply )
  15. Excellent Article!
  16. Stored queries
  17. Stored queries ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  18. Stored queries ( 4 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.