我在ASP.NET MVC3应用程序中使用Entity Framework,我正在尝试使用以下代码:
var token = "";
this.Database.ExecuteSqlCommand("exec dbo.MyUsp", new SqlParameter("token", token));
我的存储过程签名是:
CREATE PROCEDURE MyUSP(@token varchar(10) OUT)
(...)
当我使用此代码时,我收到一条错误消息,指出参数“@token”是预期但未提供。
如何告诉EF令牌参数是否用于输出?
答案 0 :(得分:23)
我最终使用它来使它工作,但我确信有更优化的方式:
var p = new SqlParameter
{
ParameterName = "token",
DbType = System.Data.DbType.String,
Size = 100,
Direction = System.Data.ParameterDirection.Output
};
var resp = this.Database.SqlQuery<String>("exec dbo.usp_GetRequestToken @token", p);
return resp.First();
答案 1 :(得分:9)
var outParam = new SqlParameter();
outParam.ParameterName = "OutPutParametname";
outParam.SqlDbType = SqlDbType.Bit;//DataType Of OutPut Parameter
outParam.Direction = ParameterDirection.Output;
db.Database.ExecuteSqlCommand("EXEC ProcedureName @Param1,@Param2 OUTPUT", new SqlParameter("Param1", value), outParam);
object outParamValue = Convert.ToBoolean(outParam.Value);
答案 2 :(得分:8)
您需要在参数中指明方向。例如,尝试这样的事情:
var p = new SqlParameter("token", token);
p.Direction = ParameterDirection.InputOutput;
this.Database.ExecuteSqlCommand("exec dbo.MyUsp", p);
答案 3 :(得分:8)
我使用以下SQL和实体框架代码解决了这个问题
SP:
ALTER PROCEDURE [dbo].[SaveSingleColumnValueFromGrid]
(
@TableName VARCHAR(200),
@ColumnName VARCHAR (200),
@CompareField VARCHAR(200),
@CompareValue VARCHAR(200),
@NewValue VARCHAR(200),
@Result INT OUTPUT
)
AS
BEGIN
DECLARE @SqlString NVARCHAR(2000),
@id INTEGER = 0;
IF @CompareValue = ''
BEGIN
SET @SqlString = 'INSERT INTO ' + @TableName + ' ( ' + @ColumnName + ' ) VALUES ( ''' + @NewValue + ''' ) ; SELECT @id = SCOPE_IDENTITY()';
EXECUTE sp_executesql @SqlString, N'@id INTEGER OUTPUT', @id OUTPUT
END
ELSE
BEGIN
SET @SqlString = 'UPDATE ' + @TableName + ' SET ' + @ColumnName + ' = ''' + @NewValue + ''' WHERE ' + @CompareField + ' = ''' + @CompareValue + '''';
EXECUTE sp_executesql @SqlString
set @id = @@ROWCOUNT
END
SELECT @Result = @id
END
实体框架代码:
public FieldUpdateResult SaveSingleColumnValueFromGrid(string tableName, string tableSetFieldName, string updatedValue, string tableCompareFieldName, string uniqueFieldValue)
{
var fieldUpdateResult = new FieldUpdateResult() ;
var isNewRecord = false;
if (string.IsNullOrWhiteSpace(uniqueFieldValue))
{
uniqueFieldValue = string.Empty;
isNewRecord = true;
}
using (var dbContext = new DBEntities())
{
var resultParameter = new SqlParameter("@Result", SqlDbType.Int)
{
Direction = ParameterDirection.Output
};
var recordsAffected = dbContext.Database.ExecuteSqlCommand("SaveSingleColumnValueFromGrid @TableName,@ColumnName,@CompareField,@CompareValue,@NewValue,@Result out",
new SqlParameter("@TableName", tableName),
new SqlParameter("@ColumnName", tableSetFieldName),
new SqlParameter("@CompareField", tableCompareFieldName),
new SqlParameter("@CompareValue", uniqueFieldValue),
new SqlParameter("@NewValue", updatedValue),
resultParameter);
fieldUpdateResult.Success = recordsAffected > 0;
if (isNewRecord)
{
fieldUpdateResult.NewId = (int)resultParameter.Value;
}
else
{
fieldUpdateResult.AffectedRows = (int)resultParameter.Value;
}
}
return fieldUpdateResult;
}
答案 4 :(得分:2)
以下是我使用DevArt驱动程序为Oracle做的事情。我有一个名为P_SID.SID_PGet的package.proc,它返回一个字符串值。 proc是:
PROCEDURE SID_PGet(io_SID OUT varchar2) is
Begin
io_SID:=GetSID; -- GetSID just goes off and gets the actual value
End;
下面是我如何调用它并检索SID值(我首先使用EF 4.1代码,这个方法在DbContext中):
/// <summary>
/// Get the next SID value from the database
/// </summary>
/// <returns>String in X12345 format</returns>
public string GetNextSId()
{
var parameter = new Devart.Data.Oracle.OracleParameter("io_SID", Devart.Data.Oracle.OracleDbType.VarChar, ParameterDirection.Output);
this.Database.ExecuteSqlCommand("BEGIN P_SID.SID_PGet(:io_SID); END;", parameter);
var sid = parameter.Value as string;
return sid;
}
答案 5 :(得分:1)
var db = new DBContext();
var outParam = new SqlParameter
{
ParameterName = "@Param",
DbType = System.Data.DbType.String,
Size = 20,
Direction = System.Data.ParameterDirection.Output
};
var r = db.Database.ExecuteSqlCommand("EXEC MyStoredProd @Param OUT",outParam );
Console.WriteLine(outParam.Value);
我看到每个人都缺少的主要部分是@Param之后需要的OUT关键字。