1

we are using vs 2012.

I wrote the below method to execute executesqlcommand on dbcontext.

  //Check lock conditions for site part vrsm
  public bool CanLock(int spvId)
  {
        SqlParameter output = new SqlParameter("editMode", SqlDbType.Bit);
        output.Direction = ParameterDirection.Output;
        SqlParameter parameter = new SqlParameter("spvId", SqlDbType.Int);
        parameter.Value = spvId;
        ExecuteProcedure("exec [dbo].[prc_SitePartVrsn_CanLock] {0}, @editMode = {1} output", parameter, output);

        return Convert.ToBoolean(output.Value);
   }

This is very old way of passing parameters. Do we have any better way of doing it in c# 4.5 vs 2012.

please help...

1 Answer 1

1

Apart from your syntax, which will throw an error for you, that's pretty much the only way to do it. I have corrected the syntax below:

SqlParameter output = new SqlParameter("editMode", SqlDbType.Bit);
output.Direction = ParameterDirection.Output;

SqlParameter parameter = new SqlParameter("spvId", SqlDbType.Int);
parameter.Value = spvId;

ExecuteProcedure("exec [dbo].[prc_SitePartVrsn_CanLock] @spvId, @editMode OUTPUT", parameter, output);

bool retVal = (bool)output.Value; // same result as using Convert.ToBoolean

edit Unless you want to use something like

var p = new SqlParameter {
    ParameterName = "paramName",
    DbType = DbType.Bit,
    Direction = ParameterDirection.Output
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.