c# – 从SQL获取数据并放入列表
发布时间:2020-12-15 08:48:05 所属栏目:百科 来源:网络整理
导读:我从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
我从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(); } 表看起来像这样, aID,bID,名称 ** >问题 ** 我被困在如何将项目添加到列表中,这也是最佳做法? 解决方法List<Fruit> fruits = new List<Fruit>(); 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); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |