Using EF, I'm trying to execute a stored procedure that returns a single string value, i.e. the status of an SQL Agent Job.
使用EF,我正在尝试执行一个返回单个字符串值的存储过程,即SQL代理作业的状态。
The stored procedure is declared as
存储过程声明为
CREATE PROCEDURE [dbo].[up_GetJobStatus](@JobStatus NVARCHAR(30) OUTPUT)
AS
-- some code omitted for brevity
SELECT @JobStatus = (
SELECT
CASE job_state
WHEN 1 THEN 'Executing'
WHEN 2 THEN 'Waiting for thread'
WHEN 3 THEN 'Between retries'
WHEN 4 THEN 'Idle'
WHEN 5 THEN 'Suspended'
WHEN 6 THEN '<unknown>'
WHEN 7 THEN 'Performing completion actions'
END
FROM @xp_results results
INNER JOIN msdb.dbo.sysjobs sj
ON results.job_id = sj.job_id
WHERE sj.job_id = @job_id)
RETURN
I have verified the stored procedure is working correct as I can execute it in query window and it returns
我已验证存储过程是否正常,因为我可以在查询窗口中执行它并返回
@JobStatus
------------
1|Idle
However when executing with EF, the param value is NULL
但是,在使用EF执行时,param值为NULL
var param = new SqlParameter
{
ParameterName = "@JobStatus",
DbType = DbType.String,
Size = 30,
Direction = System.Data.ParameterDirection.Output
};
var result = this.etlContext.Database.SqlQuery<string>("EXEC dbo.up_GetJobStatus @JobStatus OUTPUT", param);
I've also tried the ExecuteSqlCommand
method but that didn't work either.
我也尝试过ExecuteSqlCommand方法但是也没用。
Any ideas?
有任何想法吗?
3 个解决方案
#1
8
- Create stored procedure in database
- 在数据库中创建存储过程
.
。
CREATE PROCEDURE [dbo].myStoredProcName
@inputParam1 varchar(150),
@inputParam2 varchar(150),
@myOutputParamBool bit output,
@myOutputParamString varchar(100) output,
@myOutputParamInt int output
AS
BEGIN
-- sql here
END
-
Update entity model from database to include stored procedure as shown here
从数据库更新实体模型以包括存储过程,如下所示
-
Call stored proc from C# code
从C#代码调用存储过程
.
。
System.Data.Entity.Core.Objects.ObjectParameter myOutputParamBool=new System.Data.Entity.Core.Objects.ObjectParameter("myOutputParamBool",typeof(bool));
System.Data.Entity.Core.Objects.ObjectParameter myOutputParamString=new System.Data.Entity.Core.Objects.ObjectParameter("myOutputParamString",typeof(string));
System.Data.Entity.Core.Objects.ObjectParameter myOutputParamInt=new System.Data.Entity.Core.Objects.ObjectParameter("myOutputParamInt",typeof(Int32));
using (var context = new SandCryptEntities())
{
context.myStoredProcName(inputParam1, inputParam2, myOutputParamBool, myOutputParamString, myOutputParamInt);
}
bool myBool = Convert.ToBoolean(myOutputParamBool.Value);
string myString = Convert.ToString(myOutputParamString.Value);
int myInt = Convert.ToInt32(myOutputParamInt.Value);
#2
4
Here's an actual answer. Apparently there are serious issues with output parameters in entity framework when you're using DbContext/code first. This article has a good discussion and workaround: http://weblogs.asp.net/dwahlin/using-entity-framework-code-first-with-stored-procedures-that-have-output-parameters. Apparently it's supposed to be "fixed" with an update, but I haven't really seen that happen.
这是一个实际的答案。显然,当您首先使用DbContext /代码时,实体框架中的输出参数存在严重问题。本文有一个很好的讨论和解决方法:http://weblogs.asp.net/dwahlin/using-entity-framework-code-first-with-stored-procedures-that-have-output-parameters。显然它应该通过更新来“修复”,但我还没有真正看到过这种情况。
#3
0
Please review this link to execute your stored procedure and retrieve the output parameter. You will first need to import the stored procedure into your dbmx.
请查看此链接以执行存储过程并检索输出参数。您首先需要将存储过程导入dbmx。
#1
8
- Create stored procedure in database
- 在数据库中创建存储过程
.
。
CREATE PROCEDURE [dbo].myStoredProcName
@inputParam1 varchar(150),
@inputParam2 varchar(150),
@myOutputParamBool bit output,
@myOutputParamString varchar(100) output,
@myOutputParamInt int output
AS
BEGIN
-- sql here
END
-
Update entity model from database to include stored procedure as shown here
从数据库更新实体模型以包括存储过程,如下所示
-
Call stored proc from C# code
从C#代码调用存储过程
.
。
System.Data.Entity.Core.Objects.ObjectParameter myOutputParamBool=new System.Data.Entity.Core.Objects.ObjectParameter("myOutputParamBool",typeof(bool));
System.Data.Entity.Core.Objects.ObjectParameter myOutputParamString=new System.Data.Entity.Core.Objects.ObjectParameter("myOutputParamString",typeof(string));
System.Data.Entity.Core.Objects.ObjectParameter myOutputParamInt=new System.Data.Entity.Core.Objects.ObjectParameter("myOutputParamInt",typeof(Int32));
using (var context = new SandCryptEntities())
{
context.myStoredProcName(inputParam1, inputParam2, myOutputParamBool, myOutputParamString, myOutputParamInt);
}
bool myBool = Convert.ToBoolean(myOutputParamBool.Value);
string myString = Convert.ToString(myOutputParamString.Value);
int myInt = Convert.ToInt32(myOutputParamInt.Value);
#2
4
Here's an actual answer. Apparently there are serious issues with output parameters in entity framework when you're using DbContext/code first. This article has a good discussion and workaround: http://weblogs.asp.net/dwahlin/using-entity-framework-code-first-with-stored-procedures-that-have-output-parameters. Apparently it's supposed to be "fixed" with an update, but I haven't really seen that happen.
这是一个实际的答案。显然,当您首先使用DbContext /代码时,实体框架中的输出参数存在严重问题。本文有一个很好的讨论和解决方法:http://weblogs.asp.net/dwahlin/using-entity-framework-code-first-with-stored-procedures-that-have-output-parameters。显然它应该通过更新来“修复”,但我还没有真正看到过这种情况。
#3
0
Please review this link to execute your stored procedure and retrieve the output parameter. You will first need to import the stored procedure into your dbmx.
请查看此链接以执行存储过程并检索输出参数。您首先需要将存储过程导入dbmx。