【问题标题】:How to extract columns and rows from a DataSet in ASP.NET C#如何从 ASP.NET C# 中的数据集中提取列和行
【发布时间】:2012-10-15 03:47:21
【问题描述】:

*如何比较 UnitsInStock 的数量与用户想要购买的数量?*

在这里,我对我的 SQL 数据库进行了查询,以获取放入 DataSet 的列列表。 “where”子句是我当前正在浏览的项目,提取了有关该产品的所有当前信息。

我尝试使用将数据取出到数据列中,然后我现在需要将它与 int, quantity 进行比较。

        string selectionString =
        "SELECT Products.ProductID, " +
            "Products.ProductName, Categories.CategoryName, " +
            "Suppliers.CompanyName, Products.QuantityPerUnit," +
            "Products.UnitPrice, Products.UnitsInStock " +    
            "FROM Suppliers INNER JOIN (Categories  " +
            "INNER JOIN Products ON  " +
            "Categories.CategoryID = Products.CategoryID)  " +
            "ON Suppliers.SupplierID = Products.SupplierID " +
            "WHERE Products.ProductID = " +
            int.Parse(Session["Current Item"].ToString());

DataSet ds = new DataSet();//this is the dataset that keeps my columns and rows.

然后我有一个循环将除最后一个列 UnitsInStock 之外的所有列分配给文本框。 我需要处理的值是一个 int、数量。

根据用户打开的物品,每个物品的库存会有所不同。所以我打开的项目,我想知道如何将用户决定的可变数量与我从 DataSet 中提取的数量进行比较。

UnitsInStock 列的索引是6。不要担心 QuantityPerUnit,这没什么。

这里是包含 ProductName、CategoryName、CompanyName、QuantityPerUnit、UnitPrice 和 UnitsInStock 的代码,因此您可以了解如何使用数据集。

 if (ds.Tables[0].Columns.Count != 0 &&
        ds.Tables[0].Rows.Count != 0)
    {

        for (int index = 0; index < ds.Tables[0].Columns.Count - 1; index++)/// count -1, explain
        {
            labelArray[index].Text = ds.Tables[0].Columns[index].ColumnName;//named constant
            if (index == 5) // The price field
            {

                textBoxArray[index].Text = ((Decimal)ds.Tables[0].Rows[0][index]).ToString("F");
            }
            else
                textBoxArray[index].Text = ds.Tables[0].Rows[0][index].ToString();

那么,我如何将数据库检索到的数量与从文本框中收集的用户想要购买的数量进行比较。

【问题讨论】:

  • 你是说每个项目的行都有一个包含数量的列?
  • 有 ProductName、CategoryName、CompanyName、QuantityPerUnit、UnitPrice 和 UnitsInStock 列。 UnitsInstock 是第 6 列
  • 你在哪里存储用户给出的数量?

标签: c# asp.net database dataset comparison


【解决方案1】:

我想您想过滤用户给出的数量的行。 以下代码仅选择那些包含 UnitsInStock 等于用户给出的数量的行。

我假设用户给出的数量存储在 txtQuantityGivenByUser 文本框中。

var selectedRows = ds.Tables[0].Select("UnitsInStock = " + txtQuantityGivenByUser.Text);

【讨论】:

    【解决方案2】:

    在现有代码中比较它们的最简单方法是在 for 循环中添加另一个 if (index == 语句:

    if (index == 6)
    {
        int qtyUserWants = int.Parse(qtyUserWantsTextbox.Text);
        int qtyInStock = (int)ds.Tables[0].Rows[0][index];
    
        if (qtyUserWants >  qtyInStock)
        {
            // do something
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      相关资源
      最近更新 更多