DbDataReader、NextResult() 和填充多个表
Posted
技术标签:
【中文标题】DbDataReader、NextResult() 和填充多个表【英文标题】:DbDataReader, NextResult() and filling more than one table 【发布时间】:2012-07-06 22:46:35 【问题描述】:这个问题是我previous 的一个问题的延续。在不涉及太多细节的情况下,我将使用 2 个相关的一对多表填充数据集。 所以,我现在的问题是 - 为什么这段代码运行良好
public DataAgencyR_DataSet SelectOne(int id)
DataAgencyR_DataSet result = new DataAgencyR_DataSet();
using (DbCommand command = Connection.CreateCommand())
try
command.CommandText = SqlStrings.SelectDataAgencyR_SelectOne();
var param = ParametersBuilder.CreateByKey(command, "ID_DeclAgenc", id, "ID_DeclAgenc");
command.Parameters.Add(param);
Connection.Open();
using (DbDataReader reader = command.ExecuteReader())
while (reader.Read())
System.Diagnostics.Trace.WriteLine(String.Format("0-1", reader[0], reader[1]));
System.Diagnostics.Trace.WriteLine("-------------");
reader.NextResult();
while (reader.Read())
System.Diagnostics.Trace.WriteLine(String.Format("0-1", reader[0], reader[1]));
catch (DbException e)
Logger.Error(e.Message, e);
throw new DataAccessException("Error occurs while SelectOne method porcessed", e);
finally
if (Connection.State != ConnectionState.Closed) Connection.Close();
return result;
public static string SelectDataAgencyR_SelectOne()
return "SELECT a.* FROM t0_DataAgency_R a WHERE a.SetToPartners = 1 AND a.ID_DeclAgenc = @ID_DeclAgenc;" +
"SELECT c.* FROM t01_ChoiceParam_R c JOIN t0_DataAgency_R a on a.ID_DeclAgenc = c.ID_DeclAgenc WHERE SetToPartners = 1 AND a.ID_DeclAgenc = @ID_DeclAgenc";
这不是
public DataAgencyR_DataSet SelectOne(int id)
DataAgencyR_DataSet result = new DataAgencyR_DataSet();
using (DbCommand command = Connection.CreateCommand())
try
command.CommandText = SqlStrings.SelectDataAgencyR_SelectOne();
var param = ParametersBuilder.CreateByKey(command, "ID_DeclAgenc", id, "ID_DeclAgenc");
command.Parameters.Add(param);
Connection.Open();
using (DbDataReader reader = command.ExecuteReader())
result.t0_DataAgency_R.Load(reader);
reader.NextResult();
result.t01_ChoiceParam_R.Load(reader);
catch (DbException e)
Logger.Error(e.Message, e);
throw new DataAccessException("Error occurs while SelectOne method porcessed", e);
finally
if (Connection.State != ConnectionState.Closed) Connection.Close();
return result;
public static string SelectDataAgencyR_SelectOne()
return "SELECT a.* FROM t0_DataAgency_R a WHERE a.SetToPartners = 1 AND a.ID_DeclAgenc = @ID_DeclAgenc;" +
"SELECT c.* FROM t01_ChoiceParam_R c JOIN t0_DataAgency_R a on a.ID_DeclAgenc = c.ID_DeclAgenc WHERE SetToPartners = 1 AND a.ID_DeclAgenc = @ID_DeclAgenc";
在第二个示例之后,我只填写了 result.t0_DataAgency_R 表 - 但没有填写 result.t01_ChoiceParam_R。为什么会这样?
提前致谢
【问题讨论】:
【参考方案1】:DataTable.Load
自动将阅读器推进到下一个结果。因此,您应该删除对NextResult
的显式调用。
意思:
using (DbDataReader reader = command.ExecuteReader())
result.t0_DataAgency_R.Load(reader);
result.t01_ChoiceParam_R.Load(reader);
【讨论】:
如果结果集数量不定怎么办?如何检查何时停止检查下一个结果?【参考方案2】:添加数据集...我们曾经使用 SqlDataAdapter 并返回一个数据集,但没有利用任何离线功能等,因此 SqlDataReader 更适合。这是填充数据集的代码。发现这总体上快了大约 10%。
Dim s As DataSet = New DataSet()
Using reader As SqlDataReader = command.ExecuteReader()
Dim tables As New List(Of DataTable)
Do
Dim table As New DataTable()
table.Load(reader)
tables.Add(table)
s.Tables.Add(table)
Loop While Not reader.IsClosed
s.Load(reader, LoadOption.OverwriteChanges, tables.ToArray())
End Using
【讨论】:
以上是关于DbDataReader、NextResult() 和填充多个表的主要内容,如果未能解决你的问题,请参考以下文章
DataReader.NextResult 检索结果是不是总是相同的顺序
.NextResult() 方法确实给出了一个错误,指出不存在数据