检测存储过程删除是否已运行



如果我调用一个存储过程,我如何检测它在服务器上成功完成,就像现在一样,我只是在尝试捕获,这不是最好的方法。

public bool deleteTeam(Guid teamId)
{
    try
    {
        string cs = ConfigurationManager.ConnectionStrings["uniteCms"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(cs.ToString());
        // the stored procedure
        SqlCommand cmd = new SqlCommand(
            "proc_unitecms_deleteTeam", myConnection);
        // 2. set the command object so it knows
        // to execute a stored procedure
        cmd.CommandType = CommandType.StoredProcedure;
        // 3. add parameter to command, which
        // will be passed to the stored procedure
        cmd.Parameters.Add(
            new SqlParameter("@ID", teamId));
        return true;
    } catch(Exception ex)
    {
        return false;
    }
}

您可以返回受影响的行号,并在捕获异常时返回-1。

您忘记了ExecuteNonQuery。

SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
Int32 rowsAffected;
cmd.CommandText = "StoredProcedureName";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
rowsAffected = cmd.ExecuteNonQuery();
sqlConnection1.Close();

最新更新