C# DBContext executesqlcommand的SqlParameters

C# DBContext executesqlcommand的SqlParameters,c#,actionscript-3,apache-flex,flex4.5,C#,Actionscript 3,Apache Flex,Flex4.5,我们正在使用vs 2012 我编写了下面的方法来在dbcontext上执行executesqlcommand //Check lock conditions for site part vrsm public bool CanLock(int spvId) { SqlParameter output = new SqlParameter("editMode", SqlDbType.Bit); output.Direction = ParameterDi

我们正在使用vs 2012

我编写了下面的方法来在dbcontext上执行executesqlcommand

  //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);
   }
这是一种非常古老的传递参数的方法。 在c#4.5版和2012版中,我们有没有更好的方法来做到这一点


请帮助…

除了语法之外,语法会给您带来错误,这几乎是唯一的方法。我已更正以下语法:

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
编辑除非您想使用

var p = new SqlParameter {
    ParameterName = "paramName",
    DbType = DbType.Bit,
    Direction = ParameterDirection.Output
};

除了您的语法(这会给您带来错误)之外,这几乎是唯一的方法。我已更正以下语法:

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
编辑除非您想使用

var p = new SqlParameter {
    ParameterName = "paramName",
    DbType = DbType.Bit,
    Direction = ParameterDirection.Output
};