如何一次执行多条select count(1) from mytable where conditon

step_123 2009-11-06 04:25:32
请教各位:

如何一次执行多条:select count(1) from mytable where condition (每次查询条件均不相同)

数据库(sqlserver2005)(暂不考虑用存储过程)。

谢谢!
...全文
132 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
step_123 2009-11-18
  • 打赏
  • 举报
回复
谢谢各位,我改用存储过程了。
hhc123 2009-11-06
  • 打赏
  • 举报
回复
StringBuilder sql=new StringBuilder ();
把你要执行的SQL 加到sql中
然后,事务执行................
nashina 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sabty 的回复:]
SqlCommand 支持一次执行多条命令的。每条命令后加分号即可。

SqlCommand comm = new SqlCommand();

comm.CommandText = @"
select count(1) from mytable where condition;
select count(1) from mytable where condition ";
[/Quote]

在数据库里用这种方式就可以实现;
但是在C#程序里通过数据库引擎时,好像不支持封号分隔的多条sql语句的;
你可以写个函数,比如:
public List<datatable> read (List<sql>)
{
}
jiangshun 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 step_123 的回复:]
To:jiangshun

是没有关系,我只是想把问题说得更清楚一些,毕竟是要请教大家,以免耽误大家的时间。就像我上面的补充描述,实际上也跟问题无关。

其实问题只有一个:如何一次执行多条 查询语句select count(1) from table where ...
(不是update \delete\ insert )
有点啰嗦,请勿见怪。
[/Quote]

就直接写select count(1) from table where 。。。加个分号就可以,但是这样返回的是多个table
要是想在一个table中,可以用union all
select count(1) from table where 。。。 union all
select ....
step_123 2009-11-06
  • 打赏
  • 举报
回复
To:jiangshun

是没有关系,我只是想把问题说得更清楚一些,毕竟是要请教大家,以免耽误大家的时间。就像我上面的补充描述,实际上也跟问题无关。

其实问题只有一个:如何一次执行多条 查询语句select count(1) from table where ...
(不是update \delete\ insert )
有点啰嗦,请勿见怪。
sabty 2009-11-06
  • 打赏
  • 举报
回复
SqlCommand 支持一次执行多条命令的。每条命令后加分号即可。

SqlCommand comm = new SqlCommand();

comm.CommandText = @"
select count(1) from mytable where condition;
select count(1) from mytable where condition ";
step_123 2009-11-06
  • 打赏
  • 举报
回复
顺便补充一下,这个表只有7-8个字段,主键是int型,自增长,也不存在查询效率的问题,表的记录数不会超过2万条。
ViewStates 2009-11-06
  • 打赏
  • 举报
回复
select count(1) from mytable where condition
select count(1) from mytable where condition
select count(1) from mytable where condition
select count(1) from mytable where condition
select count(1) from mytable where condition
...
用DATAREADER去取,取一个就NEXTRESULT去取下一个对应条件的结果。
jiangshun 2009-11-06
  • 打赏
  • 举报
回复
一次执行多条?

condition (每次查询条件均不相同)
有撒关系?
compleat 2009-11-06
  • 打赏
  • 举报
回复
帮顶下吧,没遇到过

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧