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();
}
}
}
}
不能直接使用用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();
}
}
}
}