ADO.NET—返回多个结果集查询

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 using System.Data;
 8 using System.Data.SqlClient;
 9 
10 namespace ADO.NETDemo
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             //定义连接字符串
17             string connstring = "Server=F-PC\\SQLEXPRESS;DataBase=StudentManageDB;Uid=sa;Pwd=123456";
18             //创建连接对象
19             SqlConnection conn = new SqlConnection(connstring);
20 
21             //编写SQL语句
22             string sql = "select StudentId,StudentName from Students;select ClassId,ClassName from StudentClass";
23 
24             //创建command对象
25             SqlCommand cmd = new SqlCommand(sql, conn);
26 
27             //打开连接
28             conn.Open();
29 
30             //执行操作
31             SqlDataReader objReader = cmd.ExecuteReader();
32             //读取第一个结果集
33             while (objReader.Read())
34             {
35              Console.WriteLine(objReader["StudentId"].ToString() + "\t" + objReader["StudentName"].ToString());
36             }
37             Console.WriteLine("----------------------");
38             if (objReader.NextResult())
39             while (objReader.Read())
40              {
41              Console.WriteLine(objReader["ClassId"].ToString() + "\t" + objReader["ClassName"].ToString());
42              }
43             //关闭连接
44             objReader.Close();
45             conn.Close();
46 
47             
48             Console.ReadLine();
49 
50         }
51     }
52 }

 

posted @ 2017-07-13 15:28  一只羚  阅读(1385)  评论(0编辑  收藏  举报