微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从C#函数的存储过程返回多个输出参数

我正在使用输出参数从我的数据库获取值.

这是我的存储过程:

ALTER PROCEDURE [dbo].[sp_GetCustomerMainData] 
    -- Add the parameters for the stored procedure here
        @Reference nvarchar(100),@SubscriptionPIN nvarchar(100) OUTPUT,@SignupDate nvarchar(100) OUTPUT,@ProductCount int OUTPUT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    SET @SubscriptionPIN = 'N/A'
    SET @SignupDate = 'N/A'
    SET @ProductCount = 0

    -- Insert statements for procedure here
    IF EXISTS(SELECT [SubscriptionPIN] FROM [norton].[dbo].[Customers] WHERE [Reference] = @Reference)
    BEGIN
        SELECT TOP 1 @SubscriptionPIN = [SubscriptionPIN],@SignupDate = SignUpDate  FROM [norton].[dbo].[ProductList] WHERE [Reference] = @Reference
        SET @ProductCount = (SELECT COUNT(*) FROM [norton].[dbo].[ProductList] WHERE [Reference] = @Reference)
    END

    RETURN (@SubscriptionPIN)
    RETURN (@SignupDate)
    RETURN (@ProductCount)
END

我不确定最后的回报:

RETURN (@SubscriptionPIN)
RETURN (@SignupDate)
RETURN (@ProductCount)

另一方面,这是c#代码

using (sqlConnection con = new sqlConnection(connectionInfo))
{
    using (sqlCommand cmd = new sqlCommand("sp_GetCustomerMainData",con) { CommandType = CommandType.StoredProcedure })
    {
        cmd.Parameters.Add("@Reference",sqlDbType.NVarChar).Value = CustomerReferenceID;

        sqlParameter SubscriptionPIN = new sqlParameter("@TheCustomerID",sqlDbType.NVarChar) { Direction = ParameterDirection.Output };
        cmd.Parameters.Add(SubscriptionPIN);

        sqlParameter SignupDate = new sqlParameter("@SignupDate",sqlDbType.NVarChar) { Direction = ParameterDirection.Output };
        cmd.Parameters.Add(SignupDate);

        sqlParameter ProductCount = new sqlParameter("@ProductCount",sqlDbType.Int) { Direction = ParameterDirection.Output };
        cmd.Parameters.Add(ProductCount);

        con.open();

        try
        {
            cmd.ExecuteNonQuery();

            if (cmd.Parameters["@TheCustomerID"].Value.ToString() != "N/A")
            {
                aStatus.SubscriptionPIN = cmd.Parameters["@TheCustomerID"].Value.ToString();
                aStatus.SignupDate = cmd.Parameters["@SignupDate"].Value.ToString();
                aStatus.ProductCount = int.Parse(cmd.Parameters["@ProductCount"].Value.ToString());
                aStatus.Result = "0: Reference ID Found";
             }
             else
             {
                 aStatus.Result = "1: Reference ID does not exists";
                 return aStatus;
             }
          }
          catch (sqlException sqlExc)
          {
              foreach (sqlError error in sqlExc.Errors)
              {
                  aStatus.Result = string.Format("{0}: {1}",error.Number,error.Message);
                  return aStatus;
              }
          }
      }
}

当我运行此代码时,我收到错误

system.invalidOperationException: String[1]: the Size property has an invalid size of 0.
at System.Data.sqlClient.sqlParameter.Validate(Int32 index,Boolean isCommandProc)
at System.Data.sqlClient.sqlCommand.SetUpRPCParameters(_sqlRPC rpc,Int32 startCount,Boolean inSchema,sqlParameterCollection
parameters)
at System.Data.sqlClient.sqlCommand.buildrPC(Boolean inSchema,sqlParameterCollection parameters,_sqlRPC& rpc)
at System.Data.sqlClient.sqlCommand.RunExecuteReaderTds(CommandBehavior
cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean
async)
at System.Data.sqlClient.sqlCommand.RunExecuteReader(CommandBehavior
cmdBehavior,String
method,DbAsyncResult result)
at System.Data.sqlClient.sqlCommand.InternalExecuteNonQuery(DbAsyncResult
result,String methodName,Boolean sendToPipe)
at System.Data.sqlClient.sqlCommand.ExecuteNonQuery()

我不知道从存储过程发送许多输出参数的正确方法是什么,有人可以帮忙吗?

解决方法

您需要指定nvarchar参数的最大长度:
sqlParameter SubscriptionPIN = new sqlParameter("@TheCustomerID",sqlDbType.NVarChar,100) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(SubscriptionPIN);
sqlParameter SignupDate = new sqlParameter("@SignupDate",100) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(SignupDate);

从存储过程中删除return语句.您无需为返回的输出参数执行任何操作. (另外,您只能使用一个return语句,并且只能返回整数值.您可以使用带有ReturnValue方向的参数来获取返回的值.)

原文地址:https://www.jb51.cc/csharp/99195.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐