如何填写数据集有多个表? [英] How to fill Dataset with multiple tables?
问题描述
我试图填补数据集,其中包含2个表有一对多的关系。 我使用的DataReader来实现这一点:
I'm trying to fill DataSet which contains 2 tables with one to many relationship. I'm using DataReader to achieve this :
public DataSet SelectOne(int id)
{
DataSet result = new DataSet();
using (DbCommand command = Connection.CreateCommand())
{
command.CommandText = "select * from table1";
var param = ParametersBuilder.CreateByKey(command, "ID", id, null);
command.Parameters.Add(param);
Connection.Open();
using (DbDataReader reader = command.ExecuteReader())
{
result.MainTable.Load(reader);
}
Connection.Close();
}
return result;
}
但我有只有一个表填满。我如何实现我的目标 - 填补这两个表? 我想用的DataReader,而不是DataAdapter的,如果可能的
But I've got only one table filled up. How do I achieve my goal - fill both tables? I would like to use DataReader instead DataAdapter, if it possible
感谢您
推荐答案
如果您在发行几个select语句一条命令,可以使用NextResult方法移至DataReader的内下一个结果集:<一href="http://msdn.microsoft.com/en-us/library/system.data.idatareader.nextresult.aspx">http://msdn.microsoft.com/en-us/library/system.data.idatareader.nextresult.aspx
If you are issuing a single command with several select statements, you might use NextResult method to move to next resultset within the datareader: http://msdn.microsoft.com/en-us/library/system.data.idatareader.nextresult.aspx
我展示它如何能期待波纹管:
I show how it could look bellow:
public DataSet SelectOne(int id)
{
DataSet result = new DataSet();
using (DbCommand command = Connection.CreateCommand())
{
command.CommandText = @"
select * from table1
select * from table2
";
var param = ParametersBuilder.CreateByKey(command, "ID", id, null);
command.Parameters.Add(param);
Connection.Open();
using (DbDataReader reader = command.ExecuteReader())
{
result.MainTable.Load(reader);
reader.NextResult();
result.SecondTable.Load(reader);
// ...
}
Connection.Close();
}
return result;
}
这篇关于如何填写数据集有多个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!