Solved

multiple from recordsets SQL->C# app

Posted on 2016-08-26
4
50 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 65

Accepted Solution

by:
Jim Horn earned 500 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 42

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 42

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

803 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