Hashtable hashtable = new Hashtable(); foreach (GridViewRow r in this.GridView1.Rows) { string value = r.Cells[1].Text; hashtable[value] = "0";//这里随便放个东西 } string number = hashtable.Count;利用哈希表,把Name列的内容作为键,相同的记录有相同的键。 最后只要取得哈希表中记录的数量;
在SQL语句里处理掉不就可以了吗? SQL codeselect DISTINCT * from BOOK_ADDR
在SQL语句里处理掉不就可以了吗? SQL codeselect DISTINCT COUNT(name) from BOOK_ADDR
select DISTINCT COUNT(name) from BOOK_ADDR 呀代码怎么发错了呢这个是对的
非常感谢2位 jeremyyang824,有个问题:这个哈希方法很好,但是只能计算当前页的,如何让他统计所有页(由于GridView中有分页)gzy11 应该是select count(distinct name ) from book_addr,但是我不知道在后台如何用?之前一直用SQLDataSource连接数据库,能否详细点?多谢2位了!!!
select count(distinct name ) from book_addr 这个就是SQL语句啊,直接在后台可是执行的SQL语句 比如这个 //计算总共有多少条记录 private int CalculateRecord() { try { int recordCount; OleDbConnection oc = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath(@"~\App_Data\DataBase.aspx")); //数据库使用Northwind; oc.Open(); string OleDb = "select count(*) as [count] from Picture"; OleDbCommand cmd = new OleDbCommand(OleDb, oc); OleDbDataReader sdr = cmd.ExecuteReader(); if (sdr.Read()) { recordCount = Int32.Parse(sdr["count"].ToString()); } else { recordCount = 0; } sdr.Close(); oc.Close(); return recordCount; } catch (Exception ex) { throw new Exception(ex.Message); } }
this, may not beautiful but worksuse the column counts:select b.ID, b.Name, t.counts from book_addr b, (select count(distinct name) as counts from book_addr) t
gzy11 :你提供的方法是可以计算GridView中不重复记录的个数,但是我想先根据关键字模糊查询,然后再做统计如何做呢?多谢! 如: strSQL = "SELECT * FROM tbltest where " + DropDownList1.SelectedItem.Value + " like '%" + TextBox1.Text + "%'";
Hashtable hashtable = new Hashtable();
foreach (GridViewRow r in this.GridView1.Rows)
{
string value = r.Cells[1].Text;
hashtable[value] = "0";//这里随便放个东西
} string number = hashtable.Count;利用哈希表,把Name列的内容作为键,相同的记录有相同的键。
最后只要取得哈希表中记录的数量;
SQL codeselect DISTINCT * from BOOK_ADDR
SQL codeselect DISTINCT COUNT(name) from BOOK_ADDR
呀代码怎么发错了呢这个是对的
jeremyyang824,有个问题:这个哈希方法很好,但是只能计算当前页的,如何让他统计所有页(由于GridView中有分页)gzy11 应该是select count(distinct name ) from book_addr,但是我不知道在后台如何用?之前一直用SQLDataSource连接数据库,能否详细点?多谢2位了!!!
这个就是SQL语句啊,直接在后台可是执行的SQL语句
比如这个
//计算总共有多少条记录
private int CalculateRecord()
{
try
{
int recordCount;
OleDbConnection oc = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath(@"~\App_Data\DataBase.aspx")); //数据库使用Northwind;
oc.Open();
string OleDb = "select count(*) as [count] from Picture";
OleDbCommand cmd = new OleDbCommand(OleDb, oc);
OleDbDataReader sdr = cmd.ExecuteReader(); if (sdr.Read())
{
recordCount = Int32.Parse(sdr["count"].ToString());
}
else
{
recordCount = 0;
} sdr.Close();
oc.Close();
return recordCount;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
(select count(distinct name) as counts from book_addr) t
如:
strSQL = "SELECT * FROM tbltest where " + DropDownList1.SelectedItem.Value + " like '%" + TextBox1.Text + "%'";