asp.net-mvc 带有输出参数的 ExecuteSqlCommand
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6861737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ExecuteSqlCommand with output parameter
提问by Sam Huggill
I'm using Entity Framework in an ASP.NET MVC3 application and I'm trying to use the following code:
我在 ASP.NET MVC3 应用程序中使用实体框架,并尝试使用以下代码:
var token = "";
this.Database.ExecuteSqlCommand("exec dbo.MyUsp", new SqlParameter("token", token));
My stored proc signature is:
我存储的过程签名是:
CREATE PROCEDURE MyUSP(@token varchar(10) OUT)
(...)
When I use this code I get an error saying that parameter "@token" was expected but not supplied.
当我使用此代码时,我收到一条错误消息,指出需要但未提供参数“@token”。
How do I tell EF that the token parameter is for output?
我如何告诉 EF 令牌参数用于输出?
回答by Sam Huggill
I ended up using this to get it working, but I'm sure there's a more optimal way:
我最终使用它来让它工作,但我确信有一个更优化的方法:
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();
回答by Code Cool
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);
回答by Hector Correa
You need to indicate the direction in the parameter. For example, try something like this:
您需要在参数中指明方向。例如,尝试这样的事情:
var p = new SqlParameter("token", token);
p.Direction = ParameterDirection.InputOutput;
this.Database.ExecuteSqlCommand("exec dbo.MyUsp", p);
回答by Rahul Garg
I solved this issue with following SQL and Entity Framework code
我使用以下 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
Entity Framework Code :
实体框架代码:
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;
}
回答by CMS
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);
The main part i see everyone is missing, is the OUT keyword needed after @Param.
我看到每个人都缺少的主要部分是@Param 之后需要的 OUT 关键字。
回答by Ciaran Bruen
Below is what I do for Oracle using the DevArt driver. I have a package.proc called P_SID.SID_PGet that returns a single string value. The proc is:
下面是我使用 DevArt 驱动程序为 Oracle 做的事情。我有一个名为 P_SID.SID_PGet 的 package.proc,它返回一个字符串值。过程是:
PROCEDURE SID_PGet(io_SID OUT varchar2) is
Begin
io_SID:=GetSID; -- GetSID just goes off and gets the actual value
End;
Below is how I call it and retrieve the SID value (I'm using this with EF 4.1 code first and this method is in the DbContext):
下面是我如何调用它并检索 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;
}

