使用DataReader

DataReader类是快速的,末缓冲的,只向前移动只读游标,同时,连接到数据源,逐行读取数据。
不能直接使用用DataReader类,而是通过执行Command对象的ExecuteReader返回它的实例。
   SqlDataReader sqlReader = sqlCmd.ExecuteReader();
上面通过执行sqlCommand对象的ExecuteReader方法返回它的实例。

示例遍历DataReader

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;//导入命名空间,使能够访问这些命名空间的类
using System.Data;
namespace SQLServerProvider
{
    class Program
    {

        static void Main(string[] args)
        {
            SqlConnection thisConnection = new SqlConnection();
            thisConnection.ConnectionString =
              @"Server=localhost;Integrated security=true;database=Northwind";
            try
            {
                thisConnection.Open();
                string SQL = "SELECT ContactName FROM Customers";

                SqlCommand sqlCmd = new SqlCommand(SQL, thisConnection);
                //获得SqlDataReader实例
                SqlDataReader sqlReader = sqlCmd.ExecuteReader();
                //逐行显示记录
                while (sqlReader.Read())
                {
                    Console.WriteLine("{0}", sqlReader[0]);
                }
                sqlReader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                thisConnection.Close();
                Console.ReadLine();
            }
        }
    }
}

在DataReader中使用多个结果集:
using System;
Read()方法遍历单一的结果集,把游标放在下一记录。NextResult()方法把游标放在下一结果集,Read再从那里开始,使我们可以同时用两个或者多个查询完成数据庫查询。实现用同一个DataReader对象遍历多个数据集。
using System.Collections.Generic;
using System.Linq;
using System.Text;
//导入命名空间,使能够访问这些命名空间的类
using System.Data.SqlClient;
using System.Data;
namespace SQLServerProvider
{
    class Program
    {

        static void Main(string[] args)
        {
            SqlConnection thisConnection = new SqlConnection();
            thisConnection.ConnectionString =
              @"Server=localhost;Integrated security=true;database=Northwind";

thisConnection.Open();
string SQL = "SELECT City FROM Customers WHERE City like 'L%'" +
             "SELECT Title  FROM Employees "+
             "SELECT LastName  FROM Employees"; ;
                SqlCommand sqlCmd = new SqlCommand(SQL, thisConnection);
                SqlDataReader sqlReader = sqlCmd.ExecuteReader();
            try
            {
                do
                {
                     while (sqlReader.Read())//游标放到下一条记录
                        Console.WriteLine("{0}", sqlReader.GetSqlString(0));
                   
                }
                while (sqlReader.NextResult());//游标放到下一结果集,Read再从那里开始
               
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                thisConnection.Close();
                Console.WriteLine();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值