• SqlDataReader控制多个数据集


    using System;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace Chapter12
    {
        class MultipleResults
        {
            static void Main(string[] args)
            {
                // connection string
                string connString = @"
                                server = .;
                                integrated security = true;
                                database = northwind
                             ";
    
                // query 1
                string sql1 = @"
                            select
                               companyname,
                               contactname
                            from
                               customers
                            where
                               companyname like 'A%'
                         ";
    
                // query 2
                string sql2 = @"
                            select
                               firstname,
                               lastname
                            from
                               employees
                         ";
    
                // combine queries
                string sql = sql1 + sql2;
    
                SqlDataReader rdr = null;
               
                SqlConnection conn = null;
    
                try
                {
                     // create connection
                    conn = new SqlConnection(connString);
                    // open connection
                    conn.Open();
    
                    // create command
                    SqlCommand cmd = new SqlCommand(sql, conn);
    
                    // 执行两个sql分句后返回两个数据集
                    rdr = cmd.ExecuteReader();
    
                    // loop through result sets
                    do{
                        while (rdr.Read())
                        {
                            // Print one row at a time
                            Console.WriteLine("{0} : {1}", rdr[0], rdr[1]);
                        }
                        Console.WriteLine("".PadLeft(60, '='));
                    }
                    while (rdr.NextResult()); //SqlDataReader.NextResult()函数前进至下一个数据集。
    
                   
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error Occurred: " + e);
                }
                finally
                {
                    // close data reader
                    rdr.Close();
                    // Close connection
                    conn.Close();
                }
            }
        }
    }
    
    //所有代码来自书籍《Begining C# Databases From Novice to Professional》

  • 相关阅读:
    MySQL Workbench的安全更新模式
    IEnumerable<T>和IQueryable<T>区分
    Google 网站打不开
    使用 MVVMLight 命令绑定(转)
    使用 MVVMLight 绑定数据(转)
    安装/使用 MVVMLight(转)
    ?? 运算符(C# 参考)
    REST风格URL
    node+mysql 数据库连接池
    理解mysql执行多表联合查询
  • 原文地址:https://www.cnblogs.com/java20130722/p/3207190.html
Copyright © 2020-2023  润新知