gpt4 book ai didi

c# - c#查询结果中记录计数

转载 作者:行者123 更新时间:2023-11-30 19:39:22 25 4
gpt4 key购买 nike

如何获取查询结果中的总记录数

   SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["con1"].ConnectionString);
string sql = "select Fare , OrderAmount FROM orderitems OI INNER JOIN Orders O ON O.OrderID = OI.OrderID WHERE PaymentStatusID = 1 AND o.orderid ="+Orderid;
SqlCommand comd = new SqlCommand(sql, con1);
con1.Open();
comd.ExecuteNonQuery();

以上是我的 sql 查询,我需要结果计数,有人可以帮助我吗提前致谢。

最佳答案

ExecuteNonQuery 用于插入/更新/删除等查询。

使用ExecuteScalar相反:

string sql = "select  count(*) FROM orderitems OI INNER JOIN Orders O ON O.OrderID = OI.OrderID WHERE PaymentStatusID = 1  AND o.orderid ="+Orderid;    
SqlCommand comd = new SqlCommand(sql, con1);
con1.Open();
int count = Convert.ToInt32(comd.ExecuteScalar());

关于c# - c#查询结果中记录计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27876137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com