Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  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

Hosted by Securewebs.com
 
Home : .NET : ADO : Running Stored Queries in Access Database
 
Names - Table
Names - Table

Congratulations! you have just entered a record using stored query.

Running Stored Queries in an ASP page
Ok we are now ready to move on to running this stored query in the ASP page. Copy the following code and paste it into a new file and save it as storedproc.asp :

	<%
		' -- StoredProc.asp --
	%>
	<html>
	<head>
		<title>Running Stored Queries in Access Database</title>
		<style>p { font-family:verdana,arial; font-size:10pt;
			font-weight:bold; }</style>
	</head>
	<body><p>
	<%
		' Connection String
		' Provide relative path to your StoredProc.mdb
		' database
		Dim connStr
			connStr = "Provider=Microsoft.Jet.OLEDB.4.0;
			Data Source=" & Server.MapPath("StoredProc.mdb")

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

		' connecting to database
		con.Open connStr

		' executing stored procedure
		con.Execute "exec InsertProc 'New Name'"

		' closing connection and freeing resources
		con.Close
		Set con = Nothing
	%>
</p></body>
   </html>

Explanation
The only line of interest to us is where con.Execute "exec InsertProc 'New Name'" lies. Now place both storeproc.asp and StoredProc.mdb files in the same place. Then run storedproc.asp page in your browser. You will see nothing happening. Then go and check your database, double click 'Names' table and you will see New Name listed there.

Names - Table
Names - Table

Well done! I must say. We created an Insert stored query in Access database and first ran it from within Access and then called it from an ASP page and it ran fine.

More Stored Queries
We will create now create some more stored queries in our Access database. I will not recall all the steps of creating a stored query, that you can see in earlier pages of this article. I will only tell you what SQL statement to use in that query and then how to call it from within ASP page.

Select Stored Query
This query will select all the records from the database. Create a new query in 'Design view' and copy paste the following code into it and then save it as SelectProc :

	SELECT * FROM [Names];

Explanation
Just selects all the records from the table Names. To call it from within ASP page, edit the storedproc.asp page so that it looks like following :

	<%
		' -- StoredProc.asp --
	%>
	<html>
	<head>
		<title>Running Stored Queries in Access Database</title>
		<style>p { font-family:verdana,arial; font-size:10pt;
				font-weight:bold; }</style>
	</head>
	<body><p>
	<%
		' Connection String
		' Provide relative path to your StoredProc.mdb
		' database
		Dim connStr
			connStr = "Provider=Microsoft.Jet.OLEDB.4.0;
			Data Source=" & Server.MapPath("StoredProc.mdb")

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

		' Recordset Object
		Dim rs

			' connecting to database
			con.Open connStr

			' executing stored procedure
			Set rs = con.Execute ("exec SelectProc")

			' showing all records
			While Not rs.EOF
				Response.Write rs(0) & " " & rs(1) & "<br>"
				rs.MoveNext
			Wend

			' closing connection and freeing resources
			con.Close

			Set rs = Nothing

			Set con = Nothing
	%>
	</p></body>
	</html>

When you run storedproc.asp, it should look like following in your browser:

storedproc.asp
storedproc.asp

Next we will be creating an Update stored procedure ( Query ).

Update Stored Query
This query will update record in the Names table. Create a new query in 'Design view' and copy paste the following code into it and then save it as UpdateProc :

	UPDATE [Names] SET Name = [@newName] WHERE ID=[@IDis];

Explanation
Updates the Name field to a new name in the Names table with given ID. Thus this stored query asks for two arguments, first new name, second the ID of the Name you want updated. To call it from within ASP page, edit the storedproc.asp page so that it looks like following :

<%
		' -- StoredProc.asp --
%>
<html>
<head>
	<title>Running Stored Queries in Access Database</title>
	<style>p { font-family:verdana,arial; font-size:10pt;
			font-weight:bold; }</style>
</head>
<body><p>
<%
	' Connection String
	' Provide relative path to your StoredProc.mdb
	' database
	Dim connStr
		connStr = "Provider=Microsoft.Jet.OLEDB.4.0;
		Data Source=" & Server.MapPath("StoredProc.mdb")

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

	' Recordset Object
	Dim rs

		' connecting to database
		con.Open connStr

		' executing stored procedure
		con.Execute "exec UpdateProc 'Changed Name', 2"
		Set rs = con.Execute ("exec SelectProc")

		' showing all records
		While Not rs.EOF
			Response.Write rs(0) & " " & rs(1) & "<br>"
			rs.MoveNext
		Wend

		' closing connection and freeing resources
		con.Close
		Set rs = Nothing
		Set con = Nothing
%>
</p></body>
</html>

Notice that the only one new line of code has been added to the storedproc.asp page. This is a con.Execute statement which runs the UpdateProc stored query and changes the second name in the Names table to 'Changed Name'.

When you ran storedproc.asp before it looked like this :

storedproc.asp
storedproc.asp

Now it should look like this :

storedproc.asp
storedproc.asp
Names - Table
Names - Table

Congratulations! you have just entered a record using stored query.

Running Stored Queries in an ASP page
Ok we are now ready to move on to running this stored query in the ASP page. Copy the following code and paste it into a new file and save it as storedproc.asp :

	<%
		' -- StoredProc.asp --
	%>
	<html>
	<head>
		<title>Running Stored Queries in Access Database</title>
		<style>p { font-family:verdana,arial; font-size:10pt;
			font-weight:bold; }</style>
	</head>
	<body><p>
	<%
		' Connection String
		' Provide relative path to your StoredProc.mdb
		' database
		Dim connStr
			connStr = "Provider=Microsoft.Jet.OLEDB.4.0;
			Data Source=" & Server.MapPath("StoredProc.mdb")

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

		' connecting to database
		con.Open connStr

		' executing stored procedure
		con.Execute "exec InsertProc 'New Name'"

		' closing connection and freeing resources
		con.Close
		Set con = Nothing
	%>
</p></body>
   </html>

Explanation
The only line of interest to us is where con.Execute "exec InsertProc 'New Name'" lies. Now place both storeproc.asp and StoredProc.mdb files in the same place. Then run storedproc.asp page in your browser. You will see nothing happening. Then go and check your database, double click 'Names' table and you will see New Name listed there.

Names - Table
Names - Table

Well done! I must say. We created an Insert stored query in Access database and first ran it from within Access and then called it from an ASP page and it ran fine.

More Stored Queries
We will create now create some more stored queries in our Access database. I will not recall all the steps of creating a stored query, that you can see in earlier pages of this article. I will only tell you what SQL statement to use in that query and then how to call it from within ASP page.

Select Stored Query
This query will select all the records from the database. Create a new query in 'Design view' and copy paste the following code into it and then save it as SelectProc :

	SELECT * FROM [Names];

Explanation
Just selects all the records from the table Names. To call it from within ASP page, edit the storedproc.asp page so that it looks like following :

	<%
		' -- StoredProc.asp --
	%>
	<html>
	<head>
		<title>Running Stored Queries in Access Database</title>
		<style>p { font-family:verdana,arial; font-size:10pt;
				font-weight:bold; }</style>
	</head>
	<body><p>
	<%
		' Connection String
		' Provide relative path to your StoredProc.mdb
		' database
		Dim connStr
			connStr = "Provider=Microsoft.Jet.OLEDB.4.0;
			Data Source=" & Server.MapPath("StoredProc.mdb")

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

		' Recordset Object
		Dim rs

			' connecting to database
			con.Open connStr

			' executing stored procedure
			Set rs = con.Execute ("exec SelectProc")

			' showing all records
			While Not rs.EOF
				Response.Write rs(0) & " " & rs(1) & "<br>"
				rs.MoveNext
			Wend

			' closing connection and freeing resources
			con.Close

			Set rs = Nothing

			Set con = Nothing
	%>
	</p></body>
	</html>

When you run storedproc.asp, it should look like following in your browser:

storedproc.asp
storedproc.asp

Next we will be creating an Update stored procedure ( Query ).

Update Stored Query
This query will update record in the Names table. Create a new query in 'Design view' and copy paste the following code into it and then save it as UpdateProc :

	UPDATE [Names] SET Name = [@newName] WHERE ID=[@IDis];

Explanation
Updates the Name field to a new name in the Names table with given ID. Thus this stored query asks for two arguments, first new name, second the ID of the Name you want updated. To call it from within ASP page, edit the storedproc.asp page so that it looks like following :

<%
		' -- StoredProc.asp --
%>
<html>
<head>
	<title>Running Stored Queries in Access Database</title>
	<style>p { font-family:verdana,arial; font-size:10pt;
			font-weight:bold; }</style>
</head>
<body><p>
<%
	' Connection String
	' Provide relative path to your StoredProc.mdb
	' database
	Dim connStr
		connStr = "Provider=Microsoft.Jet.OLEDB.4.0;
		Data Source=" & Server.MapPath("StoredProc.mdb")

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

	' Recordset Object
	Dim rs

		' connecting to database
		con.Open connStr

		' executing stored procedure
		con.Execute "exec UpdateProc 'Changed Name', 2"
		Set rs = con.Execute ("exec SelectProc")

		' showing all records
		While Not rs.EOF
			Response.Write rs(0) & " " & rs(1) & "<br>"
			rs.MoveNext
		Wend

		' closing connection and freeing resources
		con.Close
		Set rs = Nothing
		Set con = Nothing
%>
</p></body>
</html>

Notice that the only one new line of code has been added to the storedproc.asp page. This is a con.Execute statement which runs the UpdateProc stored query and changes the second name in the Names table to 'Changed Name'.

When you ran storedproc.asp before it looked like this :

storedproc.asp
storedproc.asp

Now it should look like this :

storedproc.asp
storedproc.asp

Previous ( 1 Gone )( 1 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


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 to.

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