如何使用C#查找MS Access数据库中不同记录的数量?

时间:2016-12-24 11:19:26

标签: c# sql ms-access-2010

我有一个MS Access数据库,如下所示

Filing_Year     |  Name
------------------------
02/01/2008      | AAA             
02/01/2008      | AAA             
02/03/2008      | AAA             
03/01/2008      | BBB       

我需要在C#中进行查询,它将获得如下输出并将其显示在gridview

Filing_Year     | file_count
----------------------------
02/01/2008      | 2  
02/03/2008      | 1  
03/01/2008      | 1

这是我尝试过的代码:

dataGridView1.Columns.Add("Column", "FileCount");    // file count is not an element in database

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=|DataDirectory|\RMS_Database.mdb");

con.Open();

OleDbDataAdapter sda = new OleDbDataAdapter("select Filing_Year,count(*) from Checklist_Data group by Filing_Year ", con);

DataTable dt = new DataTable();
sda.Fill(dt);

BindingSource bSource = new BindingSource();
bSource.DataSource = dt;

dataGridView1.DataSource = bSource;
sda.Update(dt);

有了这个,我只看到唯一的filing_year,而不是计数。

提前致谢。非常感谢帮助。

1 个答案:

答案 0 :(得分:3)

您需要修改SQL查询,如:

SELECT Filing_Year, COUNT(Name) as file_count
FROM Checklist_Data 
GROUP BY Filing_Year, Name