2011-06-08 26 views
1
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(); 
} 

在下面的連接中出現錯誤,說明s在當前上下文中不存在。如何使用多個datareaders請幫助我。在ASP.NET中使用多個DataReader的問題(C#)

回答

2

聲明string s了興田循環

string s; 
    while (reader.Read()) 
    { 
     s = reader["Role"].ToString(); 
    } 
+0

謝謝你的......我可以在相同的連接做。 – Naresh 2011-06-08 06:03:44

+1

它的字符串變量,如果你賦值並且你可以在變量的範圍內檢索,那麼連接就沒有關係。 – 2011-06-08 06:05:00

1
 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();