How to put data from a DataSet into a list

I am trying to add data to a dataset in a list. This is my function in 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;
  }

The result in the dataset is similar to

column1 - colum2
A         1 
B         2
C         3
D         4

But I think the result is in the list:

column1 - colum2
D         1 
D         1
D         1
D         1

When I insert this dataset into another database, I only get this:

column1 - colum2
D         1 

What am I doing wrong in my function?

+3
source share
8 answers

this, ( this), . , , ( this). , .

ProductsDAL , .. this ProductDAL, .

, :


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

:

  • ProductDAL ( ), , . , ( GetDataSet() ) - , .
  • , ProductDAL, , ProductDAL, .
+2

ProductsDAL foreach. .

+2

, . ,

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

- LINQ to DataSets...

public List<ProductsDAL> GetAllProducts(string sqlQuery) {
    DataSet dsinsert = GetDataSet(sqlQuery, "tblProducts");

    return (from row in dsinsert.Tables[0].AsEnumerable()
            select new ProductsDAL {
                Product_ID = row.Field<string>("Product_ID"),
                ProductDescr = row.Field<string>("ProductDescr"),
            }).ToList();
}
+1
1. public List<ProductsDAL> GetAllProducts(string sqlQuery)
2. {
3.     DataSet dsinsert = GetDataSet(sqlQuery, "tblProducts");
4.     List<ProductsDAL> bPList = new List<ProductsDAL>();
5.     ProductsDALforeach (DataRow item in dsinsert.Tables[0].Rows)
6.     {
7.       this.Product_ID = item["Product_ID"].ToString();
8.       this.ProductDescr= item["ProductDescr"].ToString();
9.       bPList.Add(this);
10.     }
11.    return bPList;
12. }

, 9 . ( 7-9), , , - :

ProductDAL productDal = new ProductsDAL(){
                          Product_ID = item["Product_ID"].ToString(),
                          ProductDescr =  item["ProductDescr"].ToString()};
bPList.Add(productDal);
0

. . :

:

public IList<Product> GetProducts(string sortexpression)
{
  StringBuilder sql = new StringBuilder();
            sql.Append(" SELECT ProductName, ProductID");
            sql.Append("   FROM Products ");
            if (!string.IsNullOrEmpty(sortExpression))
                sql.Append(" ORDER BY " + sortExpression);

            DataTable dt = Db.GetDataTable(sql.ToString());

            return MakeProducts(dt);
}

, , Db.GetDataTable, :

 public static DataTable GetDataTable(string sql)
        {
            using (DbConnection connection = factory.CreateConnection())
            {
                connection.ConnectionString = connectionString;

                using (DbCommand command = factory.CreateCommand())
                {
                    command.Connection = connection;
                    command.CommandType = CommandType.Text;
                    command.CommandText = sql;

                    using (DbDataAdapter adapter = factory.CreateDataAdapter())
                    {
                        adapter.SelectCommand = command;

                        DataTable dt = new DataTable();
                        adapter.Fill(dt);

                        return dt;
                    }
                }
            }

sprocs, ... , MakeProducts (dt), :

   private IList<Product> MakeProducts(DataTable dt)
    {
        IList<Product> list = new List<Product>();
        foreach (DataRow row in dt.Rows)
            list.Add(MakeProduct(row));

        return list;
    }

, , :

 private Product MakeProduct(DataRow row)
        {
            int productId = int.Parse(row["ProductId"].ToString());
            string name = row["ProductName"].ToString();

            return new Product(productId, name);
        }
0
source

Will there be a next job?

public List<ProductsDAL> GetAllProducts(string sqlQuery) 
{ 
    return GetDataSet(sqlQuery, "tblProducts").
        Tables[0].Rows.Cast<System.Data.DataRow>().
        Select(p => new ProductsDAL()
        {
            Product_ID = p["Product_ID"].ToString(),
            ProductDescr = p["ProductDescr"].ToString()
        }).ToList();    
} 
0
source

You can try like this

public static List<AttendenceManual> PreocessData(DataSet data)
        {
            List<AttendenceManual> _AttendenceManualList = new List<AttendenceManual>();

            for (int i = 0; i < data.Tables[0].Rows.Count; i++)
            {
                AttendenceManual Student = new AttendenceManual();
                Student.StrEmpID = data.Tables[0].Rows[i]["F2"].ToString();
                _AttendenceManualList.Add(Student);
            }

            return _AttendenceManualList;
        }
0
source

Source: https://habr.com/ru/post/1736361/


All Articles