我从SQL获取数据并将其放在列表中。这就是我现在正在尝试的
public class Fruit //custom list { public string aID { get;set; } // can be more then 1 public string bID { get;set; } // only 2 but different aID public string name { get;set; } // only 1 for selection of aID and bID }
这就是我从sql获取数据的方式,
var Fruitee = new Fruit(); using (SqlConnection cn = new SqlConnection(CS())) { cn.Open(); SqlCommand sqlCommand= new SqlCommand("SELECT * FROM myTable", cn); SqlDataReader reader = sqlCommand.ExecuteReader(); while (reader.Read()) { Fruitee.add(reader["aID"], reader["bID"],reader["name"]) // ??? not sure what to put here as add is not available } cn.Close(); }
table看起来像这样
aID,bID,name
**
我被困在如何将项目添加到列表中,这 也是最佳实践吗?
List fruits = new List();
while (reader.Read()) { Fruit f = new Fruit(); f.aID = (string) reader["aID"]; f.bID = (string) reader["bID"]; f.name = (string) reader["name"]; fruits.Add(f); }