从表中选择Count

时间:2014-12-13 15:41:24

标签: c# asp.net sql-server

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

代码:

   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;


   }

 }

2 个答案:

答案 0 :(得分:0)

我猜你使用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();

答案 1 :(得分:-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();
                }
            }
        }
相关问题