简体   繁体   English

SqlParameters在Repository工作单元模式中

[英]SqlParameters in Repository unit of work pattern

In my Repository.cs, I have the below method 在我的Repository.cs中,我有以下方法

/*** Execute stored procedure ***/
public virtual void ExecuteProcedure(String procedureCommand, params SqlParameter[] sqlParams)
{
  Ctxt.Database.ExecuteSqlCommand(procedureCommand, sqlParams);
}

Im calling it from my client repository as: 我从我的客户端存储库调用它:

public bool CanLock(int spvId)
{
  SqlParameter[] parameter = { new SqlParameter ("spvId", spvId) };
  bool isLock = ExecuteProcedure("Exec prc_SitePartVrsn_CanLock {0}", parameter);
  return false;
}

Im getting error: 我得到错误:

ExecuteProcedure has invalid arguments. ExecuteProcedure具有无效参数。

Can somebody suggest how to pass parameters. 有人可以建议如何传递参数。

Also, prc_SitePartVrsn_CanLock has an output parameter, please also advise how can I pass output parameter and get the value to use in my client repository? 另外, prc_SitePartVrsn_CanLock有一个输出参数,请同时告诉我如何传递输出参数并获取在我的客户端存储库中使用的值?

You should try this: 你应该试试这个:

public bool CanLock(int spvId)
{
  SqlParameter[] parameter = { new SqlParameter ("spvId", spvId) };
  bool isLock = ExecuteProcedure("Exec prc_SitePartVrsn_CanLock @spvId", parameter);
  return false;
}

You have to name the parameter in the sql command. 您必须在sql命令中命名该参数。

I have solved this using below: 我用下面的方法解决了这个问题

    public bool CanLock(int spvId)
    {
        SqlParameter parameter = new SqlParameter("spvId", SqlDbType.Int);
        parameter.Value = spvId;

        ExecuteProcedure("Exec prc_SitePartVrsn_CanLock {0}", parameter);
        return false;
    }

Howerver is there any better way to handle this, like creating an array of parameters at one go ? Howerver有没有更好的方法来处理这个问题,比如一次创建参数数组?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM