Using stored procedure to fill dataset
Hi, can I use a stored procedure to fill a DataSet besides using DataAdapter?
If it can, how?
Thanks in advance.
[120 byte] By [
Dasensa] at [2007-11-11 8:17:11]

# 2 Re: Using stored procedure to fill dataset
SQL code (SelectCommand, DeleteCommand, etc) for SqlDataAdapter is hardcoded, so if I want to make a little change to the SQL code (change sorting from ASC to DESC) I have to recompile my app and redistribute it again.
But if using stored procedure, just to make the changes on the SQL server. No need to recompile and redistribute app again.
And in the future, I am planning to add some logic before fetching data and it will be a stored procedure.
# 3 Re: Using stored procedure to fill dataset
Uhm, a bit of confusion here. Sure you can use a stored procedure to fill a dataset, but you'll still use a SqlDataAdapter. You'll just use a SqlCommand for a stored procedure as the SelectCommand. If the stored procedure returns multiple result sets you need to set up TableMappings before calling Fill.
Rune
# 4 Re: Using stored procedure to fill dataset
If you just want to call a SQL Server sp from ADO.NET there is an example below:
http://support.microsoft.com/kb/306574/
# 5 Re: Using stored procedure to fill dataset
You have two ways of doing the same -
1.Add the following code in the stored procedure
CREATE PROCEDURE sp_TBL1 AS
select * from TBL1;
GO
Drag a DataGrid control from the toolbox to the form & add the following code to the Load event of the form
Dim DA As New SqlDataAdapter("SelData", SqlConnection1)
'SelData - Stored Proc
DA.SelectCommand.CommandType = CommandType.StoredProcedure
DA.TableMappings.Add("TBL1", "DS")
Dim DS As New DataSet ' Change this name to match .xsd file name.
DA.Fill(DS, "DS")
DataGrid1.DataSource = DS
DataGrid1.DataMember = "DS"
2. Use an ORM framework to simply drag and drop it in. I use DevForce Express and that works well for me.