C # Read multiple result sets in a stored procedure

Source: Internet
Author: User

C # Read multiple result sets in a stored procedure

-- SQL Server test environment setup:

 

Create database Test; goUSE [Test]GOif OBJECT_ID('Tab','U') is not nulldrop table TabgoCREATE TABLE [dbo].[Tab]([ID] [int] identity(1,1) NOT NULL,[name] [sysname] NOT NULL,)goif OBJECT_ID('Tab2','U') is not nulldrop table Tab2goCREATE TABLE [dbo].[Tab2]([ID] [int] IDENTITY(1,1) NOT NULL,[TabID] [int] NOT NULL,[Name2] [nvarchar](50) NULL) ON [PRIMARY]GO
-- Create a stored procedure:

 

 

if OBJECT_ID('P3','P') is not nulldrop procedure P3gocreate procedure P3asselect * from Tab;select * from Tab2;
-- Open Visual Studio-create a project-select console application]

 

 

#region Using Directivesusing System;using System.Data;using System.Data.SqlClient;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;#endregionnamespace TestReadingStoreProc{    class Program    {        static void Main(string[] args)        {            SqlConnection thisConnection = new SqlConnection(@Server=(Local);Database=Test;User ID=sa;Password=1);              SqlCommand thisCommand = thisConnection.CreateCommand();            thisCommand.CommandType = CommandType.StoredProcedure;            thisCommand.CommandText = P3;            SqlDataAdapter thisAdapter = new SqlDataAdapter(thisCommand);            DataSet thisDataSet = new DataSet();            thisAdapter.Fill(thisDataSet);            if (thisDataSet.Tables.Count > 0)            {                Console.WriteLine(Table Name:{0}Table Rows:{1},thisDataSet.Tables[0].TableName, thisDataSet.Tables[0].Rows.Count);                Console.WriteLine(Table Name:{0}Table Rows:{1}, thisDataSet.Tables[1].TableName, thisDataSet.Tables[1].Rows.Count);            }            thisConnection.Close();            Console.ReadKey();        }    }}


 

---- Press F5 to run the result:


 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.