码迷,mamicode.com
首页 > Windows程序 > 详细

C# 读取在存储过程多结果集

时间:2015-11-08 19:29:05      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:

--SQL Server 测试环境搭建:

Create database Test; 
go
USE [Test]
GO
if OBJECT_ID(‘Tab‘,‘U‘) is not null
	drop table Tab
go
CREATE TABLE [dbo].[Tab](
	[ID] [int] identity(1,1) NOT NULL,
	[name] [sysname] NOT NULL,
)
go
if OBJECT_ID(‘Tab2‘,‘U‘) is not null
	drop table Tab2
go
CREATE 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 null
	drop procedure P3
go
create procedure P3
as
select * from Tab;
select * from Tab2;
--打开Visual Studio—创建项目—选择【控制台应用程序】

#region Using Directives
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#endregion

namespace 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运行结果:

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

C# 读取在存储过程多结果集

标签:

原文地址:http://blog.csdn.net/roy_88/article/details/49719499

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!