【问题标题】:select Count from Table从表中选择计数
【发布时间】:2014-12-13 15:41:23
【问题描述】:

我的应用程序需要帮助,因为我试图在每次搜索后显示产品总数!我做了一个选择语句,每次我使用单选按钮列表和一个搜索框搜索产品时,每次从我的表 Product 中显示一行。搜索可以是用户名或产品 ID,然后用户可以选择该行并将其从网格视图中删除。我还放置了一个标签,该标签应该显示剩余的产品数量。这个标签应该给我网格视图中的产品数量!我的问题是我使用的标签只显示第一,它只生成记录,我需要显示产品的总数。

代码:

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.UI;
   using System.Web.UI.WebControls;

    public partial class Admin_AdminUserControl_Product : System.Web.UI.UserControl 
    {
       DataBaseConn Db = new DataBaseConn();

      Basket _b = new Product(); 
    protected void Page_Load(object sender, EventArgs e)
   {


         // Label shows only one record at a time
         lblQueue.Text = _b.Queue().ToString();


    }



  protected void btnSearch_Click(object sender, EventArgs e)
   {
     grid.DataSource = _b.SelectRow(rdoField.SelectedValue,txtSearch.Text);
     grid.DataBind();
      lblMsg.Text = "";
      btnRemove.Enabled = false;
      grid.SelectedIndex = -1;
  }
  protected void btnRemove_Click(object sender, EventArgs e)
  {
      if (_b.RemoveProduct(grid.SelectedRow.Cells[1].Text))

        lblMsg.Text = "Product is removed";
      else
          lblMsg.Text = "Unable to remove a Product!";
     grid.DataBind();
     btnRemove.Enabled = false;
  }
    protected void grid_SelectedIndexChanged(object sender, EventArgs e)
  {

      btnRemove.Enabled = true;
 }

类产品

  public DataTable Data(string txtField, string Value)
  {

    string SQL = String.Format("Select * from Product where {0} like'%{1}%'", txtField, Value);
    return Data(SQL);

}
public DataTable Data(string Query)
{
    try
    {
        return Db.RunQuery(Query);
    }
    catch
    {
        return new DataTable();
    }

 }

 public bool RemoveProduct(string ProductId)
{

    this.ProductID = ProductId;
    return Delete();
}

public DataTable SelectRow(string Field, string Value)
 {

    string sql = string.Format("SELECT TOP 1 * FROM Product where {0} like '%{1}%' ORDER BY 
       ProductID ASC", Field, Value);
    return SelectRow(sql);

 }

 public DataTable SelectRow(string Query)
 {
    try
    {
        return Db.RunQuery(Query);
    }
    catch
    {
        return new DataTable();
    }

 }
   //This label should show number of all products i.e from 0- any number
     public int Queue()
   {
    int customers;
    String sql = String.Format("Select Count(*) from Product");
    customers = Db.RunQuery(sql).Rows.Count;
    return customers;


   }

 }

【问题讨论】:

标签: c# asp.net sql-server


【解决方案1】:

我从您对 DataTables 的使用中猜测 Db.RunQuery 正在返回一个 DataTable?如果是这样的话,那么

.Rows.Count

将只是您的查询返回的行数。在这种情况下只有一个.. 因此,如果返回的确实是 DataTable,您将像这样访问数据

customers = Convert.ToInt32(dt.Rows[0][0])

但这也只是假设 RunQuery 正在执行基本填充,返回数据表,而不是对数据表执行其他操作。

对查询执行 ExecuteScalar 也可能更简单 http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar%28v=vs.110%29.aspx

//I'm guessing this is something you'd add to your Db class. The ability to ExecuteScalar
    cmd.CommandText = "Select Count(*) from Product";
     customers = (Int32) cmd.ExecuteScalar();

【讨论】:

  • 那么我的代码会是什么样子?我还需要运行 Query 吗?
  • 关于您最初的问题,即为什么您一直获得第一而不是结果。如果替换 customers = Db.RunQuery(sql).Rows.Count;与 customers = Convert.ToInt32(dt.Rows[0][0]) 应该会给你正确的结果。但是您应该研究一下 ExecuteScalar,因为这是从查询中获取结果的更好方法
  • 我的代码有效,但不会刷新,从表中删除记录有延迟!我从来没有使用过 ExcuteScalar,所以第一次生成它对我来说有点困难:(
  • 我不关注...你问题的最后一部分说“这个标签应该给我网格视图中的产品数量!我的问题是我使用的标签只显示我的第一它只生成记录,我需要显示产品的总数。”它显示第一,因为您访问的是 DataTable 行数,而不是查询的结果。审查 ADO.NET 可能对您有好处,我认为这会让您更轻松codeproject.com/Articles/361579/…
  • 我希望我有足够的积分来截屏并在这里发布!我读了这篇文章,但仍然无法弄清楚解决方案:(
【解决方案2】:

使用以下代码:

protected void datalistcatagory()
    {

        //SqlDataAdapter da = new SqlDataAdapter("Select Catagory_ID,Catagory_Name from tbl_catagory where Catagory_ID=Catagory_ID", con);

        using (SqlCommand cmd = new SqlCommand("Select (c.Catagory_ID), COUNT(c.Catagory_ID) As CID, c.Catagory_Name from tbl_NewPost sc join tbl_catagory c on c.Catagory_ID=sc.Catagory_ID Group by c.Catagory_Name, c.Catagory_ID Order by CID DESC"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {


                        Label4.Text = dt.Rows.Count.ToString();

                    }
                    Repeater1.DataSource = dt;
                    Repeater1.DataBind();
                }
            }
        }

【讨论】:

  • 这段代码和原问题有关吗?您从NewPosttbl_catagory 中选择原始问题中从未提及的数据。您正在更新不在原始问题中的 Ui 元素。没有解释:这个答案是关于SelectCommand的用法吗?因为它不能是 Rows.Count 的例子,因为这就是 2014 年的答案。
猜你喜欢
  • 2014-02-23
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2016-12-22
  • 1970-01-01
相关资源
最近更新 更多