转自  http://blog.csdn.net/limlimlim/article/details/8626898

注意:当SQL语句中出现两条Select语句,例如:string sql = "select * from Employees;select * from TblClass";
可以用SqlDataReader的NextResult()来接收多个结果集
使用
do
{
}
while(reader.NextResult())

static void Main(string[] args)
        {

string constr = "Data Source=zxtiger;Initial Catalog=itcastcn;Integrated Security=True";
            using (SqlConnection con = new SqlConnection(constr))
            {
                string sql = "select * from Employees;select * from TblClass";
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    con.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        do
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    //循环列
                                    for (int i = 0; i < reader.FieldCount; i++)
                                    {

//根据索引,获取列名
                                        //reader.GetName(i);
                                        //获取当前列的数据类型
                                        string dbType = reader.GetDataTypeName(i);
                                        switch (dbType)
                                        {
                                            case "varchar":
                                            case "nvarchar":
                                            case "char":
                                            case "nchar":
                                                Console.Write(reader.GetString(i) + "\t");
                                                break;
                                            case "int":
                                                Console.Write(reader.GetInt32(i) + "\t");
                                                break;
                                        }
                                    }
                                    Console.WriteLine();
                                }
                            }
                        } while (reader.NextResult()); //当多个结果集的时候可以调用NextResult

}
                }
            }
            Console.WriteLine("ok");
            Console.ReadKey();
        }

05-17 22:15