简体   繁体   English

使用SqlHelper.ExecuteDataSet()和获取返回值执行SQL Server存储过程

[英]Execute SQL Server Stored Procedure with SqlHelper.ExecuteDataSet() and Get Return Value

I have an SQL Server Stored Procedure . 我有一个SQL Server Stored Procedure This procedure have an output parameter. 此过程具有输出参数。 On my C#.NET Appliaction, I execute this procedure via SqlHelper.ExecuteDataSet() and it returns a query result DataSet . 在我的C#.NET Appliaction上,我通过SqlHelper.ExecuteDataSet()执行此过程,它返回一个查询结果DataSet How can I get the output parameter from the stored procedure procedure while using SqlHelper.ExecuteDataSet() . 如何在使用SqlHelper.ExecuteDataSet()时从存储过程过程中获取输出参数。 Some article said that I need to use SqlHelper.ExecuteNonQuery() but I need the DataSet too. 有些文章说我需要使用SqlHelper.ExecuteNonQuery()但我也需要DataSet

This is my code: 这是我的代码:

        public DataSet GetDataPerTable(string spName, string a, string b, out int c)
        {
            try
            {
                c = 0;

                SqlParameter[] spParameter = new SqlParameter[3];

                spParameter[0] = new SqlParameter("@a", SqlDbType.Char, 4);
                spParameter[0].Direction = ParameterDirection.Input;
                spParameter[0].Value = a;

                spParameter[1] = new SqlParameter("@b", SqlDbType.Char, 1);
                spParameter[1].Direction = ParameterDirection.Input;
                spParameter[1].Value = b;

                spParameter[2] = new SqlParameter("@c", SqlDbType.Int);
                spParameter[2].Direction = ParameterDirection.ReturnValue;                
                c = Convert.ToInt32(spParameter[2].Value);

                return SqlHelper.ExecuteDataset(Configuration.MyConnectionString, CommandType.StoredProcedure, spName, spParameter);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

My c variable always return 0. Any Idea? 我的c变量总是返回0.任何想法? Thanks in advance :) 提前致谢 :)

My Procedure is about like this: 我的程序是这样的:

CREATE PROCEDURE [dbo].SPR_MyProcedure (@a Char(4), @bChar(1), @c Int Output)  
SELECT * 
FROM MyTable
Set @c = 1

@c isn't the a return value, it's an output parameter @c不是返回值,它是输出参数

Change 更改

spParameter[2].Direction = ParameterDirection.ReturnValue; 

to

 spParameter[2].Direction = ParameterDirection.Output;

Also, you need to set your c variable after the call to ExecuteDataset eg: 此外,您需要在调用ExecuteDataset之后设置c变量,例如:

DataSet dataset = SqlHelper.ExecuteDataset(Configuration.MyConnectionString, CommandType.StoredProcedure, spName, spParameter);
c=(int)spParameter[2];
return dataset;     

You have to use ParameterDirection.Output for parameter c . 您必须为参数c使用ParameterDirection.Output see Get output parameter value in ADO.NET 请参阅ADO.NET中的获取输出参数值

ParameterDirection.ReturnValue is a return value of the whole stored procedure, 0 by default or a value specified in RETURN statement. ParameterDirection.ReturnValue是整个存储过程的返回值,默认为0或RETURN语句中指定的值。

SqlHelper.ExecuteDataSet doesnt return output parameter. SqlHelper.ExecuteDataSet不返回输出参数。 see this link 看到这个链接

http://www.mediachase.com/documentation/fileuploader/Api/Mediachase.FileUploader.SqlHelper.ExecuteDataset_overload_5.html http://www.mediachase.com/documentation/fileuploader/Api/Mediachase.FileUploader.SqlHelper.ExecuteDataset_overload_5.html

for output parameter in every example they r using ExecuteNonQuery see these aticles for output prameter 对于使用ExecuteNonQuery的每个示例中的输出参数,请参阅这些aticles以获取输出参数

http://forums.asp.net/t/360456.aspx/1 http://forums.asp.net/t/360456.aspx/1

http://www.codeproject.com/Articles/15666/Data-Access-Application-Block-NET-2-0-Get-Return-V http://www.codeproject.com/Articles/15666/Data-Access-Application-Block-NET-2-0-Get-Return-V

i hope it can help u 我希望它可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM