简体   繁体   中英

SqlParameters in Repository unit of work pattern

In my Repository.cs, I have the below method

/*** 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.

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?

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.

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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