使用c#.net通过select语句从数据库中检索数据 [英] Retrieval of data from database through select statement using c#.net

查看:301
本文介绍了使用c#.net通过select语句从数据库中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,里面有3个俱乐部舞蹈俱乐部,戏剧俱乐部,科学俱乐部。我有一个查询来计算一个学生是数据库的一部分俱乐部。当我在sql管理工作室运行查询时,它工作得非常好。

当我将查询添加到c#时。 net使用ExecuteReader()建立连接并检索数据。这里出现问题。当我运行代码并在gridview中显示行时我不算数,但得到



id name count

1. xyz 1

2. xyz 1

3. pqr 1



即xyz是一部分2个俱乐部,因此2行



i想要(从sql管理工作室输出)



1. xyz 2

1. pqr 1

I have a database where there are 3 clubs dance club,drama club,science club. i have a query to count the number of clubs a student is a part of from the database.when i run the query in sql management studio it works perfectly fine.
When i add the query to c#.net making a connection and retrieving data using ExecuteReader(). Here the problem occurs.When i run the code and display the rows in gridview i do not get count but get as

id name count
1. xyz 1
2. xyz 1
3. pqr 1

i.e xyz is a part of 2 clubs and hence 2 rows

i want (ouput from sql management studio)

1. xyz 2
1. pqr 1

推荐答案

我认为你需要Group By子句。请查看以下链接



GROUP BY声明 [ ^ ]





如下所示。

I think you need the Group By clause. Please check below link

GROUP BY Statement[^]


something like below.
SELECT ID, NAME , COUNT (STUDENTID) FROM CLUB   GROUP BY ID 


这篇关于使用c#.net通过select语句从数据库中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆