sql NextResult()多个结果集

 

转自  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();
        }

转载于:https://www.cnblogs.com/nanxiaoxiang/p/6443769.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值