Solved

multiple from recordsets SQL->C# app

Posted on 2016-08-26
4
71 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 66

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 43

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 43

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

615 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