Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Result from Select Query in stored procedure to a List

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.

like image 801
muttley91 Avatar asked Sep 19 '13 17:09

muttley91


People also ask

Can a stored procedure return a result set?

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.

How can we return a value in stored procedure?

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.

How can I return multiple values from a stored procedure in SQL?

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.


2 Answers

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. :)

like image 168
Hitesh Avatar answered Oct 11 '22 21:10

Hitesh


 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(); 
like image 38
Vinay Kumrawat Avatar answered Oct 11 '22 22:10

Vinay Kumrawat