測試數據:Northwind
鏈接地址: https://files.cnblogs.com/files/louiszh/NorthWind.zip
首先創建一個測試存儲過程:
IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE id = object_id ('pr_test')) DROP PROCEDURE pr_test go CREATE PROCEDURE pr_test AS SELECT TOP 4 * FROM Customers SELECT TOP 4 * FROM Employees GO EXEC pr_test
返回兩個結果集:

在C#代碼中通過DataSet集合獲取存儲過程結果集:
using System; using System.Data; using System.Data.SqlClient; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string connStr = "Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=root"; SqlConnection conn = null; conn = new SqlConnection(connStr); conn.Open(); string sql = "exec pr_test"; SqlDataAdapter sda = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); sda.Fill(ds); DataTable dt_customer = ds.Tables[0]; DataTable dt_employee = ds.Tables[1]; Console.WriteLine(dt_customer.Rows.Count); } } }
查看 DataSet是包含兩個Table的:

