I'm writing a stored procedure that currently contains only a SELECT
query. It will be expanded to do a number of other things, which is why it has to be a stored procedure, but for now, it is a simple query.
Something like this:
SELECT name, occupation, position FROM jobs WHERE ...
I'm looking to return the results of this query to be used in C#. I want to add it to a list so that I can bind it to a GridView component.
I don't know how to go about this, though. If I have to insert it into a list after returning all selected data, then that's alright, I just need to know how to properly return the data so that I can do that.
If I can return it in a format that can be popped right into a list, though, that would be ideal.
In addition to returning output parameters, a stored procedure can return a result set (that is, a result table associated with a cursor opened in the stored procedure) to the application that issues the CALL statement. The application can then issue fetch requests to read the rows of the result set cursor.
Return Value in SQL Server Stored Procedure In default, when we execute a stored procedure in SQL Server, it returns an integer value and this value indicates the execution status of the stored procedure. The 0 value indicates, the procedure is completed successfully and the non-zero values indicate an error.
In order to fetch the multiple returned values from the Stored Procedure, you need to make use of a variable with data type and size same as the Output parameter and pass it as Output parameter using OUTPUT keyword. You can also make use of the Split function to split the comma separated (delimited) values into rows.
In stored procedure, you just need to write the select query like the below:
CREATE PROCEDURE TestProcedure AS BEGIN SELECT ID, Name FROM Test END
On C# side, you can access using Reader, datatable, adapter.
Using adapter has just explained by Susanna Floora.
Using Reader:
SqlConnection connection = new SqlConnection(ConnectionString); command = new SqlCommand("TestProcedure", connection); command.CommandType = System.Data.CommandType.StoredProcedure; connection.Open(); SqlDataReader reader = command.ExecuteReader(); List<Test> TestList = new List<Test>(); Test test = null; while (reader.Read()) { test = new Test(); test.ID = int.Parse(reader["ID"].ToString()); test.Name = reader["Name"].ToString(); TestList.Add(test); } gvGrid.DataSource = TestList; gvGrid.DataBind();
Using dataTable:
SqlConnection connection = new SqlConnection(ConnectionString); command = new SqlCommand("TestProcedure", connection); command.CommandType = System.Data.CommandType.StoredProcedure; connection.Open(); DataTable dt = new DataTable(); dt.Load(command.ExecuteReader()); gvGrid.DataSource = dt; gvGrid.DataBind();
I hope it will help you. :)
SqlConnection connection = new SqlConnection(ConnectionString); command = new SqlCommand("TestProcedure", connection); command.CommandType = System.Data.CommandType.StoredProcedure; connection.Open(); DataTable dt = new DataTable(); dt.Load(command.ExecuteReader()); gvGrid.DataSource = dt; gvGrid.DataBind();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With