--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 (null,[name] [sysname] not NULL,) goif object_id (' Tab2 ', ' U ') are not nulldrop table Tab2gocreat E TABLE [dbo]. [TAB2] ([ID] [int] IDENTITY () not null,[tabid] [int.] not null,[name2] [nvarchar] () 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 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);D atabase=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}\ntable rows:{1}", THISDA Taset.tables[0]. TableName, Thisdataset.tables[0]. Rows.Count); Console.WriteLine ("Table name:{0}\ntablE rows:{1} ", Thisdataset.tables[1]. TableName, Thisdataset.tables[1]. Rows.Count); } thisconnection.close (); Console.readkey (); } }}
----Press F5 to run the result:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C # Reads multiple result sets in a stored procedure