我从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); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。