使用带参数的Database.ExecuteSqlCommand的正确语法是什么?

时间:2021-03-20 02:17:11

I use Entity Framework 4.2 and want to call a stored procedure that has input parameters. I'm using Database.ExecuteSqlCommand to call the stored procedure.

我使用Entity Framework 4.2并希望调用具有输入参数的存储过程。我正在使用Database.ExecuteSqlCommand来调用存储过程。

However, the documentation is lacking in the correct syntax for the call in order to map the parameters correctly. My google-foo is failing me, and any help will be appreciated.

但是,为了正确映射参数,文档缺少正确的调用语法。我的谷歌foo让我失望,任何帮助将不胜感激。

I.e. I have a procedure

即我有一个程序

procedure SetElementFrequency
  @ElementTypeID integer,
  @Frequency float
as ...

I've tried calling it with

我试过用它来调用它

Database.ExecuteSqlCommand("exec SetElementFrequency @p0 @p1", 
                            elementType, frequency);

and

Database.ExecuteSqlCommand("exec SetElementFrequency {0} {1}", 
                            elementType, frequency);

but they both fail with the error Incorrect syntax near '@p1'.

但他们都失败了错误语法'@ p1'附近的错误语法。

3 个解决方案

#1


16  

Depending on your underlying database provider, you can use either of the following.

根据您的基础数据库提供程序,您可以使用以下任一方法。

Database.ExecuteSqlCommand(
    "exec SetElementFrequency {0}, {1}",
    elementType, frequency); 

or

要么

Database.ExecuteSqlCommand("exec SetElementFrequency ?, ?", elementType, frequency); 

You may also specify elementType and frequency as DbParameter-based objects to provide your own names via the ParameterName property.

您还可以将elementType和frequency指定为基于DbParameter的对象,以通过ParameterName属性提供您自己的名称。

#2


2  

Try something like this:

尝试这样的事情:

context.Database.ExecuteSqlCommand("delete MasterSmsCampaignCertificateInfo where      MasterSmsCampaignGuid = @p0 and CertificateId = @p1",
TheCampaignGuid,
certInfo.CertificateId);

Take a look at this similar question: ExecuteSqlCommand with output parameter Best regards

看看这个类似的问题:ExecuteSqlCommand带输出参数Best Concer

#3


2  

var sql = @"Update [User] SET FirstName = {0} WHERE Id = {1}";
ctx.Database.ExecuteSqlCommand(sql, firstName, id);

#1


16  

Depending on your underlying database provider, you can use either of the following.

根据您的基础数据库提供程序,您可以使用以下任一方法。

Database.ExecuteSqlCommand(
    "exec SetElementFrequency {0}, {1}",
    elementType, frequency); 

or

要么

Database.ExecuteSqlCommand("exec SetElementFrequency ?, ?", elementType, frequency); 

You may also specify elementType and frequency as DbParameter-based objects to provide your own names via the ParameterName property.

您还可以将elementType和frequency指定为基于DbParameter的对象,以通过ParameterName属性提供您自己的名称。

#2


2  

Try something like this:

尝试这样的事情:

context.Database.ExecuteSqlCommand("delete MasterSmsCampaignCertificateInfo where      MasterSmsCampaignGuid = @p0 and CertificateId = @p1",
TheCampaignGuid,
certInfo.CertificateId);

Take a look at this similar question: ExecuteSqlCommand with output parameter Best regards

看看这个类似的问题:ExecuteSqlCommand带输出参数Best Concer

#3


2  

var sql = @"Update [User] SET FirstName = {0} WHERE Id = {1}";
ctx.Database.ExecuteSqlCommand(sql, firstName, id);