目前,我有这个工作正常:
using (connection = new SqlConnection("connection string here"))
{
using (command = new SqlCommand(@"select * from tbl1", connection))
{
connection.Open();
using (reader = command.ExecuteReader())
{
while (reader.Read())
{
int ColIndex1 = reader.GetOrdinal("col_1");
int ColIndex2 = reader.GetOrdinal("col_2");
Console.Write(reader.GetString(ColIndex1);
Console.Write(" - ");
Console.Write(reader.GetString(ColIndex2);
Console.Write(Environment.NewLine);
}
}
}
}
我有另一个查询,我单独运行,但第二个查询需要第一个查询,这意味着我最终运行第一个查询两次。为避免这种情况,如果我将命令行更改为:
using (command = new SqlCommand(@"select * from tbl1; select * from tbl2", connection))
如何将每个查询放入单独的列表中?我理解如何将单个查询放入列表中,即:
public class Data
{
public int ColumnIndex1 { get; set; }
public int ColumnIndex2 { get; set; }
}
List<Data> list = new List<Data>();
list.Add(new Data(ColIndex1, ColIndex2));
第一个查询用于在硬盘上创建目录。然后,第二个查询使用第一个查询,然后将文件添加到创建的目录中。
答案 0 :(得分:2)
using (connection = new SqlConnection("connection string here"))
{
using (command = new SqlCommand(@"select * from tbl1", connection))
{
connection.Open();
using (reader = command.ExecuteReader())
{
while (reader.Read())
{
// read first grid
}
if(reader.NextResult())
{
while (reader.Read())
{
// read second grid
}
}
}
}
}
但是,我强烈建议使用辅助工具,例如,通过&#34; dapper&#34;:
List<FirstType> first;
List<FirstType> second;
using(var multi = connection.QueryMultiple(sql, args))
{
first = multi.Read<FirstType>().ToList();
second = multi.Read<SecondType>().ToList();
}
答案 1 :(得分:0)
我认为您需要调查NextResult
界面上的IDataReader
方法。这允许您移动多个结果集。
http://msdn.microsoft.com/en-us/library/system.data.idatareader.nextresult(v=vs.110).aspx