Skip to content

Instantly share code, notes, and snippets.

@appforest
Created January 5, 2014 01:53
Show Gist options
  • Save appforest/8263299 to your computer and use it in GitHub Desktop.
Save appforest/8263299 to your computer and use it in GitHub Desktop.
C# - Calling a Stored Procedure. Using a dataset to get the data.
string CS = ConfigurationManager.ConnectionStrings["test_connectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlDataAdapter da = new SqlDataAdapter("testProcedure3", con); // Using a Store Procedure.
//SqlDataAdapter da = new SqlDataAdapter("SELECT 'this is a test text' as test", con); To use a hard coded query.
da.SelectCommand.CommandType = CommandType.StoredProcedure; // Comment if using hard coded query.
DataSet ds = new DataSet(); // Definition: Memory representation of the database.
da.SelectCommand.Parameters.AddWithValue("@ggg", 95); // Repeat for each parameter present in the Store Procedure.
da.Fill(ds); // Fill the dataset with the query data
GridView1.DataSource = ds; // Set the dataso... meh, you get it.
GridView1.DataBind();
//TextBox1.Text=GridView1.Rows[0].Cells[0].Text; If you want to pass the data to another element.
}
@sin2akshay
Copy link

I realize this is too old. But can you answer, what will happen if your stored procedure returns a cursor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment