C# 讀取在預存程序多結果集

來源:互聯網
上載者:User

--SQL Server 測試環境搭建:

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
--建立預存程序:

if OBJECT_ID('P3','P') is not nulldrop procedure P3gocreate procedure P3asselect * from Tab;select * from Tab2;
--開啟Visual Studio—建立項目—選擇【控制台應用程式】

#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}\nTable Rows:{1}",thisDataSet.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();        }    }}


-- --按F5運行結果:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.