如何检查mysql删除查询是否成功
问题描述:
大家好,我是一名c#初学者.我有一个SQL查询.
Hi all, I am a beginner c# developer. I have a sql query.
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("DELETE FROM ranks WHERE rank_id NOT IN (SELECT DISTINCT(rank_id) FROM rank_transaction_type) AND rank_id = @rank_id",con);
如何检查删除查询是否成功,并在"if"语句中使用它.并存储在变量中.
例如:
如果(queryworked == true)
{
}
其他
{
}
任何帮助将非常感激.在此先感谢您.
How can you check if the delete query was successful and use it in ''if'' statement. And store in variable.
for example:
if (queryworked == true)
{
}
else
{
}
Any help would be much appreciated. Thanking you in advance.
答
在执行查询时(例如,使用ExecuteNonQuery
^ ])具有指示受影响的行数的返回类型.您可以使用此值
例如
When you execute your query (for e.g. using theExecuteNonQuery
method[^]) has a return type that indicates the number of rows affected. You can use this value
E.g.
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected >0)
{
}
else
{
}