检测是否存储了proc delete

时间:2015-11-10 11:10:53

标签: c# sql asp.net

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

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 个答案:

答案 0 :(得分:2)

您可以返回受影响的行号,并在发生异常时返回-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();