Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ASP.NET(C#)中使用多个数据读取器时出现问题_C#_.net_Asp.net_Sql_Sqldatareader - Fatal编程技术网

在ASP.NET(C#)中使用多个数据读取器时出现问题

在ASP.NET(C#)中使用多个数据读取器时出现问题,c#,.net,asp.net,sql,sqldatareader,C#,.net,Asp.net,Sql,Sqldatareader,我在下面的连接中发现错误,说当前上下文中不存在s。如何使用多个数据读取器请帮助我。声明字符串s退出循环 SqlConnection conn1 = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true"); SqlCommand COMM = new SqlCommand("select Role from Login where User_Name='admin'", conn1); c

我在下面的连接中发现错误,说当前上下文中不存在
s
。如何使用多个数据读取器请帮助我。

声明
字符串s
退出循环

SqlConnection conn1 = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");

SqlCommand COMM = new SqlCommand("select Role from Login where User_Name='admin'", conn1);
conn1.Open();

SqlDataReader reader = COMM.ExecuteReader();
while (reader.Read())
{
    string s = reader["Role"].ToString();
}
reader.Close();
conn1.Close();

SqlConnection conn = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");
SqlCommand comm = new SqlCommand("Select * from FileUpload where UploadedBy='"+NAME+"'",conn);

try
{
    conn.Open();

    SqlDataReader rdr = comm.ExecuteReader();
    if (s.Equals("admin"))
    {
        GridView1.DataSource = rdr;
        GridView1.DataBind();
    }

    if(s.Equals("teacher"))
    {
         GridView2.DataSource = rdr;
         GridView2.DataBind();
    }

    rdr.Close();
    //reader.Close();
}
catch
{
    conn.Close();
}

谢谢…我可以在同一个连接中使用它吗。它的字符串变量,如果你赋值,你可以在变量的范围内检索,它与连接无关。
  string s;
  while (reader.Read())
     {
         s = reader["Role"].ToString();
     }
      SqlConnection conn1 = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");
      SqlCommand COMM = new SqlCommand("select Role from Login where User_Name='admin'", conn1);
      conn1.Open();
      SqlDataReader reader = COMM.ExecuteReader();
      string s = String.Empty;
      while (reader.Read())
          s = reader["Role"].ToString();