A
stored procedure is a named collection of SQL statements that are stored
in the database. To the client a stored procedure acts similar to a
function. The stored procedure is called by name, can accept parameter
values passed in, and can return parameter values back to the client.
There are many advantages to incorporating stored procedures into your
application logic including:
- Shared application logic among various client applications
- Faster execution
- Reduced network traffic
- Improved database security
The purpose of
this article is to demonstrate how stored procedures can be used in
conjunction with the SqlDataAdapter in order to fill and update data
contained in a DataSet.
Note: In order to
complete the activities outlined in this article you must have Visual
Studio .NET installed and access to SQL Server 2000 with the Pubs
database installed.
private void btnGetAuthors_Click(System.Object sender, System.EventArgs e)
{
DataSet DS = null;
SqlConnection MyConnection = default(SqlConnection);
SqlDataAdapter MyDataAdapter = default(SqlDataAdapter);
//Create a connection to the SQL Server.
MyConnection = new SqlConnection("server=(local);database=pubs;Trusted_Connection=yes");
//Create a DataAdapter, and then provide the name of the stored procedure.
MyDataAdapter = new SqlDataAdapter("GetAuthorsByLastName", MyConnection);
//Set the command type as StoredProcedure.
MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
//Create and add a parameter to Parameters collection for the stored procedure.
MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@au_lname", SqlDbType.VarChar, 40));
//Assign the search value to the parameter.
MyDataAdapter.SelectCommand.Parameters("@au_lname").Value = Strings.Trim(txtLastName.Text);
//Create and add an output parameter to Parameters collection.
MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@RowCount", SqlDbType.Int, 4));
//Set the direction for the parameter. This parameter returns the Rows returned.
MyDataAdapter.SelectCommand.Parameters("@RowCount").Direction = ParameterDirection.Output;
DS = new DataSet();
//Create a new DataSet to hold the records.
MyDataAdapter.Fill(DS, "AuthorsByLastName");
//Fill the DataSet with the rows returned.
//Get the number of rows returned, and then assign it to the Label control.
//lblRowCount.Text = DS.Tables(0).Rows.Count().ToString() & " Rows Found!"
lblRowCount.Text = MyDataAdapter.SelectCommand.Parameters(1).Value + " Rows Found!";
//Set the data source for the DataGrid as the DataSet that holds the rows.
Grdauthors.DataSource = DS.Tables["AuthorsByLastName"].DefaultView;
//Bind the DataSet to the DataGrid.
//NOTE: If you do not call this method, the DataGrid is not displayed!
Grdauthors.DataBind();
MyDataAdapter.Dispose();
//Dispose of the DataAdapter.
MyConnection.Close();
//Close the connection.
}
For more information, see the following topics in the
Microsoft .NET Framework Software Development Kit (SDK) documentation:
Overview of ADO.NET
http://msdn.microsoft.com/en-us/library/h43ks021.aspx
ADO.NET DataSet
http://msdn.microsoft.com/en-us/library/zb0sdh0b%28VS.71%29.aspx
Using .NET Data Providers to Access Data
http://msdn2.microsoft.com/en-us/library/s7ee2dwt%28vs.71%29.aspx
For more general information about ADO.NET or Visual Basic .NET,
refer to the following MSDN newsgroups:
http://msdn.microsoft.com/newsgroups/default.aspx?query=dotnet.framework.adonet&dg=&cat=en-us-msdn&lang=en&cr=US&pt=&catlist=774F24A2-F71F-425F-AC2B-DC48AB0DA5C9&dglist=&ptlist=&exp=&sloc=en-us
http://msdn.microsoft.com/newsgroups/default.aspx?query=dotnet.languages.vb&dg=&cat=en-us-msdn&lang=en&cr=US&pt=&catlist=774F24A2-F71F-425F-AC2B-DC48AB0DA5C9&dglist=&ptlist=&exp=&sloc=en-us
For more information, see the following book:
Wyke, R. Allen, and Sultan Rehman and Brad Leupen. http://www.microsoft.com/mspress/books/sampchap/4798.aspx. Microsoft Press, 2001.
For more information, see the following Microsoft Training &
Certification course:
http://www.microsoft.com/learning/syllabi/en-us/2389Bfinal.mspx
For additional
information about how to perform this task by using Microsoft Active Server
Pages, click the article number below to view the article in the Microsoft
Knowledge Base:
http://support.microsoft.com/kb/300488/EN-US
How To Run SQL Stored Procedures from an ASP Page