使用一个reader对象从sql数据库中读取多个结果集

时间:2011-02-26 11:15:13

标签: c# .net sql sql-server

我可以使用相同的阅读器来读取两个结果集的结果吗?

string checktrigger =

 "select name from sys.triggers where name = 'Insertion_in_ITEMS_table' " +
 "select object_name(parent_obj)from sysobjects where id = 

object_id('Insertion_in_ITEMS_table')";

SqlCommand check_cmd = new SqlCommand(checktrigger, _conn);
SqlDataReader check_reader = check_cmd.ExecuteReader();

string triggername = check_reader.GetString(0);
string tablename = check_reader.GetString(1);

if (triggername.Length != 0)
{
  MessageBox.Show(triggername + "CREATED SUCCESSFULLY" + "ON TABLE " +  triggername + tablename);
}    

这给出了索引越界的错误 我应该使用数组?返回结果集

1 个答案:

答案 0 :(得分:3)

用户reader.Read()获取下一条记录,reader.NextResult()切换到结果集中的下一个结果。

while (check_reader.Read())
{
    // get rows from the first select
}

// switch to next
check_reader.NextResult();

while (check_reader.Read())
{
    // get rows from the second select
}