Wednesday, March 28, 2012

how to get the results of sp_stored_procedures in C# app?

The result of sp_stored_procedures and many others is a result set, a table, but this procedure and others return only an integer. How do I get the result set in C# code? I need a general idea how it is done.

Thanks.

Are you looking for something like this:

using (SqlConnection cxn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Master;Integrated Security=True"))
{
cxn.Open();
SqlCommand cmd = new SqlCommand("sp_stored_procedures", cxn);
SqlDataReader rdr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

while (rdr.Read())
{
for (int i = 0; i < rdr.FieldCount; i++)
{
Console.Write(rdrIdea.ToString() + "\t");
}
Console.WriteLine();
}

cmd.Dispose();
}

|||

If the procedure provides more than one resultsset, you will have to switch between the resultsets to retrieve the resultset you want to get. ( As a procedure can have more than one returned table ) E.g. If you use a datareader you can switch to the next next result.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||

Jeff Papiez - MSFT wrote:

Are you looking for something like this:

using (SqlConnection cxn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial ();
}

Thank you very much, Jeff.

|||

Jens K. Suessmeyer wrote:

If the procedure provides more than one resultsset, you will have to switch between the resultsets to retrieve the resultset you want to get. ( As a procedure can have more than one returned table ) E.g. If you use a datareader you can switch to the next next result.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

Jens, thanks a lot.

No comments:

Post a Comment