我需要获取更新的行的第一列值。但是,当我在Management Studio中运行查询Update ClaimDetails set sStatus='False' OUTPUT inserted.slno as Slno where inVoiceNo='******' and sStatus='True'
时,它会返回正确的值。但是,当我尝试使用Executescalar()
值返回得到空输出子句返回值,但执行计数器在C#中返回空值
我的代码:
bool isupdated = false;
int modified=0;
try
{
string updateqry = "Update ClaimDetails set [email protected] OUTPUT inserted.slno as Slno where [email protected] and sStatus='True'";
SqlCommand cmd = new SqlCommand(updateqry, con);
cmd.Parameters.AddWithValue("@sStatus", sStatus);
cmd.Parameters.AddWithValue("@inVoiceNo", inVoiceNo);
connect();
if (cmd.ExecuteNonQuery() > 0)
{
isupdated = true;
//modified = (int)cmd.ExecuteScalar();
object a = cmd.ExecuteScalar();
if (a != null)
modified = Convert.ToInt32(a);
}
}
catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
finally { disconnect(); }
return modified;
当我用modified = (int)cmd.ExecuteScalar();
它给了我一个异常错误,所以我用object
“它给了我一个异常错误”它给你什么例外? –