假设我有一个存储过程,但我无法控制它(也无法访问第三方数据库)。
那么,我如何知道它是否运行成功?
BEGIN
Update USR
Set usr_psswrd = @NewPassword
where
usr_usrnme = @UserName and usr_psswrd = @OldPassword
END
我知道如何在存储过程中使用select
语句获取行并读取这些行,但我不知道如何检查此存储过程是否成功。
到目前为止,我所做的事情是不起作用的。存储过程有效,因为密码确实更改了,但事后我不知道该怎么做。
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Create the command and set its properties.
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "USP_ChangePassword";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@UserName", SqlDbType.VarChar).Value = email;
command.Parameters.Add("@OldPassword", SqlDbType.VarChar).Value = oldPW;
command.Parameters.Add("@NewPassword", SqlDbType.VarChar).Value = newPW;
try
{
// Open the connection and execute the reader.
connection.Open();
command.ExecuteNonQuery();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
success = true;
}
reader.Close();
}
catch (SqlException ex)
{
System.Diagnostics.Debug.Write("SqlException Error " + ex.Number + ": " + ex.Message);
}
catch (InvalidOperationException ex)
{
System.Diagnostics.Debug.Write("Invalid Op Error: " + ex.Message);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write("Error: " + ex.Message);
}
finally
{
connection.Close();
}
}
catch (Exception ex){System.Diagnostics.Debug.Write("Error: " + ex.ToString());throw;}
此外,你的SqlCommand
和SqlDataReader
需要在using
块中。 - John Saunders