我有以下代码用于处理来自两个表的数据,但我似乎无法使用两个SqlDataReaders来处理它。知道如何更改它以使其工作吗?
//SqlConnection cn = valid SQL Server 2008 connection
using (SqlCommand cmd = new SqlCommand("SELECT * FROM table1", cn))
{
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
int nVal1 = rdr.GetInt32(0);
int nVal2 = rdr.GetInt32(1);
//...
int nValN = rdr.GetInt32(N);
//Now I process the data read ...
//After processing I need to use the results
//to look up data from another table using
//this item's data
using (SqlCommand cmd2 = new SqlCommand("SELECT * FROM table2 WHERE " + strSQLCondition, cn))
{
//But code below doesn't let me create another reader
using (SqlDataReader rdr2 = cmd2.ExecuteReader())
{
while (rdr2.Read())
{
//Process results
}
}
}
}
}
}
答案 0 :(得分:3)
默认情况下,您只能在连接上使用一个活动数据集。你有几个选择。想到的三个是