storedqueries.aspx
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim myParameter As OleDbParameter
Dim myDataReader As OleDbDataReader
' Create connection and set connection string
myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " _
& "Data Source=" & Server.MapPath("storedqueries.mdb") & ";")
' Create a new command object
myCommand = New OleDbCommand()
' Set the properties of the command so that it uses our
' connection and knows the name of the stored query to run.
myCommand.Connection = myConnection
myCommand.CommandText = "GetNameInfoById"
myCommand.CommandType = CommandType.StoredProcedure
' Create our input parameter Id, tell it it's
' and integer and set it's value
myParameter = myCommand.CreateParameter()
With myParameter
.ParameterName = "Id"
.Direction = ParameterDirection.Input
.OleDbType = OleDbType.Integer
.Value = 1
End With
' Add parameter to our command
myCommand.Parameters.Add(myParameter)
' Open the connection to the database
myConnection.Open()
' Execute our command and have it return a datareader.
' We then pull the data we want out of that.
' Could just as easily databind to a DataGrid or do
' whatever else you want to do with your data.
myDataReader = myCommand.ExecuteReader()
' Sort of a pointless loop since there's only one
' row, but you get the point.
Do While myDataReader.Read()
lblFirstName.Text = myDataReader.GetString(0)
lblLastName.Text = myDataReader.GetString(1)
Loop
' Close our reader
myDataReader.Close
' Close our connection
myConnection.Close()
End Sub
</script>
<html>
<head>
<title>ASP.NET Stored Access Queries Sample</title>
</head>
<body>
<p>Ran the stored query: <strong>GetNameInfoById</strong>.</p>
<p>Passed it one parameter:<br />
<strong>Id</strong> = 1<br />
</p>
<p>
It returned a DataReader which I used to print out this name:
<strong>
<asp:Label id="lblFirstName" runat="server" />
<asp:Label id="lblLastName" runat="server" />
</strong>
</p>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/storedqueries_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>