2013-04-01 11 views
10

Chciałem odczytać wszystkie dane z tabeli (zawierającej 3 wiersze) i dodać wszystkie dane do ogólnej kolekcji. Z kolekcji i wana powiązać z gridview.Odczytywanie danych z tabeli SQL DataBase do ogólnej kolekcji

Kod wyświetlany poniżej działa, ale tylko w ostatnim wierszu jest wyświetlany 3 razy w gridview.Can pomóc me.Am początkujący

protected void Page_Load(object sender, EventArgs e) 
{ 
    List<Student> listid = new List<Student>(); 
    Student stud = new Student(); 
    SqlConnection con = new SqlConnection("........"); 
    string sql = "select * from StudentInfo"; 
    con.Open(); 
    SqlCommand cmd = new SqlCommand(sql, con); 
    SqlDataReader dr = cmd.ExecuteReader(); 
    while (dr.Read()) 
    { 
     stud.Studid = Convert.ToInt32(dr["StudId"]); 
     stud.StudName = dr["StudName"].ToString(); 
     stud.StudentDept = dr["StudentDept"].ToString(); 
     listid.Add(stud);    
    } 
    GridView1.DataSource = listid; 
    GridView1.DataBind(); 
} 
public class Student 
{ 
    private int studid; 
    public int Studid 
    { 
     get { return studid; } 
     set { studid = value; } 
    }  
    private string studName; 
    public string StudName 
    { 
     get { return studName; } 
     set { studName = value; } 
    } 
    private string studentDept; 
    public string StudentDept 
    { 
     get { return studentDept; } 
     set { studentDept = value; } 
    } 

Wyjście jest tak:

enter image description here

Odpowiedz

15

Musisz instancję swój obiekt wewnątrz pętli while
W przeciwnym razie trzeba będzie te same dane w zbiorze
więc kod powinien być

protected void Page_Load(object sender, EventArgs e) 
{ 
    List<Student> listid = new List<Student>(); 
    SqlConnection con = new SqlConnection("........"); 
    string sql = "select * from StudentInfo"; 
    con.Open(); 
    SqlCommand cmd = new SqlCommand(sql, con); 
    SqlDataReader dr = cmd.ExecuteReader(); 
    while (dr.Read()) 
    { 
     Student stud = new Student(); 
     stud.Studid = Convert.ToInt32(dr["StudId"]); 
     stud.StudName = dr["StudName"].ToString(); 
     stud.StudentDept = dr["StudentDept"].ToString(); 
     listid.Add(stud);    
    } 
    GridView1.DataSource = listid; 
    GridView1.DataBind(); 
} 

Także to nie jest dobra praktyka, aby korzystać jednocześnie do czytnika danych lub bezpośrednio otwarte połączenie
Powinieneś użyć instrukcji using.

using(SqlConnection con = new SqlConnection("connection string")) 
{ 

    con.Open(); 

    using(SqlCommand cmd = new SqlCommand("SELECT * FROM SomeTable", connection)) 
    { 
     using (SqlDataReader reader = cmd.ExecuteReader()) 
     { 
      if (reader != null) 
      { 
       while (reader.Read()) 
       { 
        //do something 
       } 
      } 
     } // reader closed and disposed up here 

    } // command disposed here 

} //connection closed and disposed here 
2

W pętli DataReader podczas tworzenia pętli nowy uczeń dla każdego wiersza w tabeli bazy danych:

while (dr.Read()) 
{ 
var stud = new Student(); 
stud.Studid = Convert.ToInt32(dr["StudId"]); 
stud.StudName = dr["StudName"].ToString(); 
stud.StudentDept = dr["StudentDept"].ToString(); 
listid.Add(stud); 
} 
Powiązane problemy