C# stored procedure check if success

时间:2017-10-12 09:39:18

标签: c# stored-procedures

I have a store procedure which inserts, deletes or updates rows in my db and doesn't return any value. I am calling it from my C# code like this:

DAL.CDataContext dc = new DAL.CDataContext();
dc.MySproc();

How can I check that it run successfully?

1 个答案:

答案 0 :(得分:1)

Use try catch. if there is no exception, then the query was ran successfully, then return true if sucess, return false if it isnt example

public static bool InsertObFile(PreApprove p)
{
    var command = new SqlCommand();
    command.CommandText = "ObInsertPreApprove";
    command.CommandType = CommandType.StoredProcedure;

    command.Parameters.AddWithValue("@EmployeeAutoId", p.EmployeeAutoId).Direction = ParameterDirection.Input;
    command.Parameters.AddWithValue("@EmployeeId", p.EmployeeId).Direction = ParameterDirection.Input;
    command.Parameters.AddWithValue("@DateFrom", p.DateFrom).Direction = ParameterDirection.Input;
    command.Parameters.AddWithValue("@DateTo", p.DateTo).Direction = ParameterDirection.Input;
    command.Parameters.AddWithValue("@Description", p.Description).Direction = ParameterDirection.Input;
    command.Parameters.AddWithValue("@CreateUserId", p.CreateUserId).Direction = ParameterDirection.Input;

    try
    {
        SqlHelper.ExecuteNonQuery(command); // this is where I run my stored procedure
        return true;
    }
    catch (Exception e)
    {

        return false;
    }

}