加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何将DataSet中的数据放入List

发布时间:2020-12-16 01:34:55 所属栏目:百科 来源:网络整理
导读:我试图将数据集中的数据添加到List中.这是我在C#中的功能 public ListProductsDAL GetAllProducts(string sqlQuery) { DataSet dsinsert = GetDataSet(sqlQuery,"tblProducts"); ListProductsDAL bPList = new ListProductsDAL();ProductsDALforeach (DataRo
我试图将数据集中的数据添加到List中.这是我在C#中的功能

public List<ProductsDAL> GetAllProducts(string sqlQuery)
  {
     DataSet dsinsert = GetDataSet(sqlQuery,"tblProducts");
     List<ProductsDAL> bPList = new List<ProductsDAL>();
ProductsDALforeach (DataRow item in dsinsert.Tables[0].Rows)
     {
       this.Product_ID = item["Product_ID"].ToString();
       this.ProductDescr= item["ProductDescr"].ToString();
       bPList.Add(this);
     }
     return bPList;
  }

数据集中的结果如下

column1 - colum2
A         1 
B         2
C         3
D         4

但我认为列表中的结果是:

column1 - colum2
D         1 
D         1
D         1
D         1

当我将这组数据插入另一个数据库时,我只得到这个:

column1 - colum2
D         1

我的功能在做什么?

解决方法

您需要在foreach语句中创建一个新的ProductsDAL.你刚刚更新了同一个.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读