这是我的SQL查询(我正在使用SQL Server Management Studio)和C#进行开发。
代码完美无缺。但我只想计算受影响的行数。
我已经尝试了几个计数等步骤但是它没有用。
SELECT
LevelDetails.App_ID, LevelDetails.Intro_ID, LevelDetails.Side,
LevelDetails.SLevel,
Top_up_details.topupdate, Top_up_details.Recharge_epin,
Top_up_details.epin, Top_up_details.app_id AS Expr1
FROM
LevelDetails
INNER JOIN
Top_up_details ON LevelDetails.App_ID = Top_up_details.app_id
WHERE
(LevelDetails.Intro_ID = '1001')
答案 0 :(得分:2)
试试这个:
SELECT count(*)
FROM LevelDetails INNER JOIN Top_up_details
ON LevelDetails.App_ID = Top_up_details.app_id
WHERE (LevelDetails.Intro_ID = '1001')
答案 1 :(得分:0)
你看起来像这样吗。
您可以添加COUNT(LevelDetails.App_ID) OVER() As rowcount
以及为您提供总行数的选择部分。
SELECT
LevelDetails.App_ID, LevelDetails.Intro_ID, LevelDetails.Side,
LevelDetails.SLevel,
Top_up_details.topupdate, Top_up_details.Recharge_epin,
Top_up_details.epin, Top_up_details.app_id AS Expr1,
COUNT(LevelDetails.App_ID) OVER() As rowcount // gives you the total count also
FROM
LevelDetails
INNER JOIN
Top_up_details ON LevelDetails.App_ID = Top_up_details.app_id
WHERE
(LevelDetails.Intro_ID = '1001')
答案 2 :(得分:0)
@OP这是您从评论中获取的代码。
dtc = g1.return_dt("SELECT count(*) as app_c FROM LevelDetails
INNER JOIN Top_up_details ON LevelDetails.App_ID = Top_up_details.app_id
WHERE (LevelDetails.Intro_ID = '" + Session["appid"] + "')");
if (dtc.Rows.Count > 0)
{
Session["app_count"] = dt.Rows[0]["app_c"].ToString();
}
我认为在上面的代码中您遗漏了Session["appid"].ToString()
,但最好在应用null
之前检查ToString()
。这是一个例子:
if(Session["appid"] != null)
{
dtc = g1.return_dt("SELECT count(*) as app_c FROM LevelDetails
INNER JOIN Top_up_details ON LevelDetails.App_ID = Top_up_details.app_id
WHERE (LevelDetails.Intro_ID = '" + Session["appid"].ToString() + "')");
if (dtc.Rows.Count > 0)
{
Session["app_count"] = dt.Rows[0]["app_c"].ToString();
}
}
另一种解决方案:
假设您的DataTable
dtc
使用问题中提到的查询,那么您只需检查表格中返回的行数:
string sQuery = @" SELECT
LevelDetails.App_ID, LevelDetails.Intro_ID, LevelDetails.Side,
LevelDetails.SLevel, Top_up_details.topupdate,
Top_up_details.Recharge_epin, Top_up_details.epin,
Top_up_details.app_id AS Expr1
FROM
LevelDetails
INNER JOIN
Top_up_details ON LevelDetails.App_ID = Top_up_details.app_id
WHERE
(LevelDetails.Intro_ID = '1001')";
DataTable dtc = g1.return_dt(sQuery);
int iTotalRows = dtc.Rows.Count;
否则,要从数据库中获取计数,您可以使用sqlcommand.ExecuteScalar()