C#:检索非参数化存储过程查询的输出参数?
我有这个存储过程
CREATE PROCEDURE [dbo].[TestProcedure]
@param1 int = 0
,@param2 int = 0
,@total_sales int = 5 OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SET @total_sales = @total_sales * 5
SELECT * FROM SomeTable
END
和 C# 中的这个字符串
string strSQL = @"
DECLARE @RC int
DECLARE @param1 int
DECLARE @param2 int
DECLARE @total_sales int
-- TODO: Set parameter values here.
SET @param1 = 1
SET @param2 = 2
EXECUTE @RC = [TestDB].[dbo].[TestProcedure]
@param1
,@param2
,@total_sales OUTPUT";
现在我想检索输出值,但不参数化输入查询!
我尝试了这个:
using (System.Data.SqlClient.SqlCommand cmd = (System.Data.SqlClient.SqlCommand)idbConn.CreateCommand())
{
cmd.CommandText = strSQL;
cmd.Transaction = (System.Data.SqlClient.SqlTransaction)idbtTrans;
iAffected = cmd.ExecuteNonQuery();
idbtTrans.Commit();
string strOutputParameter = cmd.Parameters["@total_sales"].Value.ToString();
Console.WriteLine(strOutputParameter);
} // End Using IDbCommand
这会引发异常(参数@total_sales不在参数列表中)。
如何在非参数化存储过程调用中检索输出参数不参数化查询?
I have this stored procedure
CREATE PROCEDURE [dbo].[TestProcedure]
@param1 int = 0
,@param2 int = 0
,@total_sales int = 5 OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SET @total_sales = @total_sales * 5
SELECT * FROM SomeTable
END
And this string in C#
string strSQL = @"
DECLARE @RC int
DECLARE @param1 int
DECLARE @param2 int
DECLARE @total_sales int
-- TODO: Set parameter values here.
SET @param1 = 1
SET @param2 = 2
EXECUTE @RC = [TestDB].[dbo].[TestProcedure]
@param1
,@param2
,@total_sales OUTPUT";
And now I want to retrieve the output value, but without parametrizing the input query !
I tried this:
using (System.Data.SqlClient.SqlCommand cmd = (System.Data.SqlClient.SqlCommand)idbConn.CreateCommand())
{
cmd.CommandText = strSQL;
cmd.Transaction = (System.Data.SqlClient.SqlTransaction)idbtTrans;
iAffected = cmd.ExecuteNonQuery();
idbtTrans.Commit();
string strOutputParameter = cmd.Parameters["@total_sales"].Value.ToString();
Console.WriteLine(strOutputParameter);
} // End Using IDbCommand
And this throws an exception (the parameter @total_sales is not in the parameter list).
How can I retrieve an output parameter in a non-parametrized stored-procedure call WITHOUT parametrizing the query ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短的回答:你不能。
长答案:您需要以更标准的方式使用 ADO.NET,以便能够利用输出参数等功能。
Short answer: You can't.
Long Answer: You need to use ADO.NET in a more standard way to enable you to leverage things such as output parameters.
好吧,不确定这是否有帮助,但以下内容在技术上是可行的:
在您的 TSQL 脚本中,使用 RAISERROR 来引发信息性消息。您可以使用它来返回有关变量名称和值的信息。例如
,在C# 中,使用SqlConnection.InfoMessage 事件来捕获这些消息。解析返回的字符串以提取名称和值。这将是返回参数值的一种迂回方式,但它会起作用。
Well, not sure if this will help, but the following is technically possible :
In your TSQL scripts, use RAISERROR to raise informational messages. You could use this to return information about the name and value of variables. e.g.
In C#, use the SqlConnection.InfoMessage event to catch these messages. Parse the returned strings to extract the names and values. It would be a roundabout way to return parameter values, but it would work.