我需要獲取更新的行的第一列值。但是,當我在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
「它給了我一個異常錯誤」它給你什麼例外? –