从SQL获取数据,并投入列表数据、列表、SQL

2023-09-05 03:56:40 作者:是男人、就活出你的高傲。

我从SQL获取数据,并把它在列表中。这里就是我想现在,

 公共类水果//自定义列表
{
     公共字符串援助{获取;设置; } //可以更然后1
     公共字符串的出价{获取;设置; } //只有2个,但不同的援助
     公共字符串名称{获取;设置; } //仅1选择援助和出价
}
 

这是我如何获得从SQL数据,

  VAR Fruitee =新的水果();

使用(SqlConnection的CN =新的SqlConnection(CS()))
{
      cn.Open();
      的SqlCommand的SqlCommand =新的SqlCommand(SELECT * FROM mytable的,CN);
      SqlDataReader的读者= sqlCommand.ExecuteReader();
      而(reader.Read())
      {
             Fruitee.add(读卡器[援助],读者[申奥],读者[名称])// ???不知道该怎么把这里作为附加不可用
      }
      cn.Close();
}
 

表看起来是这样的,

援助,投标,名称

**

问题

**

我坚持了如何将项目添加到列表和也是它的最佳做法?

解决方案

 名单,其中,水果GT;水果=新的名单,其中,水果GT;();

而(reader.Read())
{
    水果F =新的水果();
    f.aID =(字符串)阅读器[援助];
    f.bID =(字符串)阅读器[申奥];
    f.name =(字符串)阅读器[名称];
    fruits.Add(F);
}
 
SQL获取数据库中表信息 表名 建表时间 总行数 数据大小等

I am getting data from SQL and putting it in list. here's what I am trying now,

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
}

and this is how i am getting data from 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 looks like this,

aID, bID, name

**

Problem

**

I am stuck how to add items to list and also is it best practice ?

解决方案

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);
}