简体   繁体   English

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

[英]How to find the count of distinct records in a MS Access database using C#?

I have an MS Access database as below 我有一个MS Access数据库,如下所示

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

I need a query in C# which will get me an output as below and display it in a gridview 我需要在C#中进行查询,这将为我提供如下输出并将其显示在gridview中

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

This is the code I tried: 这是我尝试的代码:

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);

With this I am seeing only unique filing_year , but not the count. 有了这个,我看到的只是唯一的filing_year ,而不是计数。

Thanks in advance. 提前致谢。 Help is really appreciated. 非常感谢您的帮助。

You need to modify your SQL Query like: 您需要像下面那样修改您的SQL查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM