• 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) + " ");
                                                    break;
                                                case "int":
                                                    Console.Write(reader.GetInt32(i) + " ");
                                                    break;
                                            }
                                        }
                                        Console.WriteLine();
                                    }
                                }
                            } while (reader.NextResult()); //当多个结果集的时候可以调用NextResult

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

  • 相关阅读:
    ●单例模式
    ●扩展方法
    ●存储过程比sql语句慢
    ●rownum() over()
    ●日期格式化
    ●sql优化
    VS建立Web网站 20141201
    ORM操作(一) 20141128
    流的操作20141104
    控件:菜单、工具栏、状态栏及TreeView的操作 20141103
  • 原文地址:https://www.cnblogs.com/nanxiaoxiang/p/6443769.html
Copyright © 2020-2023  润新知