[英]Linq to SQL ('select * from table').Count() vs (“select column from table”).Count()
[英]select Count from Table
我的應用程序需要幫助,因為我試圖在每次搜索后顯示產品總數! 我做了一個選擇語句,每次我使用單選按鈕列表和一個搜索框搜索產品時,它都會從我的表 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;
}
}
我從你對 DataTables 的使用中猜測 Db.RunQuery 正在返回一個 DataTable? 如果是這樣的話
.Rows.Count
將只是您的查詢返回的行數。 在這種情況下只有一個..所以如果返回的確實是一個 DataTable 你會像這樣訪問數據
customers = Convert.ToInt32(dt.Rows[0][0])
但這也只是假設 RunQuery 正在執行基本填充,返回數據表,而不是對數據表執行其他操作。
針對查詢http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar%28v=vs.110%29.aspx 使用ExecuteScalar 也可能更簡單
//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();
使用以下代碼:
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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.