Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

multiple from recordsets SQL->C# app

Posted on 2016-08-26
4
Medium Priority
?
88 Views
Last Modified: 2016-08-26
I have a SQL stored proc that returns multiple recordsets from multiple queries.  When I run it in SQL Mgmt Studio, it works great.  Currently I get back 3 tables with a few dozen records in each.  Correct results.

When I call this from C# code, using SqlAdapter.Fill() into a DataSet, I do indeed get 3 tables in my DataSet.  It's just that they're totally empty.

I'm using the same parameters in each call.  

So let me just ask this, right off the bat:  Is reading these multiple recordsets into a DataSet not going to ever return results?  If it should be working, is there perhaps a known wrinkle regarding multiple recordsets somewhere that I'm missing?  

Thanks
0
Comment
Question by:bamapie
  • 2
4 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 41772065
Can't speak to the C# aspects of this quesion, but a common T-SQL reason is that if there are any messages that appear in your SSMS that are not sets, such as '(123 rows updated)', that a calling application like C# will interpret as an empty set.  

To prevent this from happening you'll have to execute statements such as SET NOCOUNT ON, SET ANSI_WARNINGS OFF, etc. in your T-SQL code before executing the code that returns the sets.

You may also want to consider changing your design so that multiple sets are not called by a single C# data set.
0
 
LVL 44

Expert Comment

by:zephyr_hex (Megan)
ID: 41772077
Here's the pattern I use for this kind of scenario:

In my SQL stored procedure, I return 3 datasets using select statements:

SELECT col FROM table1;
SELECT col FROM table2;
SELECT col FROM table3;

Open in new window


Then, in C#:

Assign parameters and call the stored procedure with execute():
oDs.Execute();
if (oDs.ErrorString != "")
{
  //handle error
}
//iterate over the datasets:
foreach (DataSet ds in oDs.DataSet)
{
  //put a breakpoint here and inspect
  var table = ds.Tables[0];
 }

Open in new window

0
 
LVL 44

Expert Comment

by:zephyr_hex (Megan)
ID: 41772088
You can test Jim's theory by only returning 1 dataset from your stored procedure and see if it works.  If it doesn't, then the issue is not with multiple datasets, but something else, like not setting SET NOCOUNT ON in your stored procedure.
0
 

Author Closing Comment

by:bamapie
ID: 41772278
Thanks!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question