【问题标题】:Trouble with OUTPUT parameter in SQL SERVER 2008SQL SERVER 2008 中的 OUTPUT 参数出现问题
【发布时间】:2013-05-29 03:09:20
【问题描述】:

我有一个这样的方法:-

public List<InterestedParty> GetDealViewData(string username,Guid opportunityId, int fromIndex, int toindex, string IPStatus, Guid? JllBroker, string ipsearch,ref string sortby, ref string direction, out int totalRecords, int offerType)
{
            if (sortby == null)
                sortby = string.Empty;

            if (direction == null)
                direction = string.Empty;

            InterestedPartyMapper interestedPartyMapper = new InterestedPartyMapper();
            InterestedParty dummyIP = new InterestedParty();

            SqlParameter outParam = new SqlParameter();
            outParam.ParameterName = "@TotalRecords";
            outParam.SqlDbType = SqlDbType.Int;
            outParam.Direction = ParameterDirection.Output;

            SqlParameter sortParam = new SqlParameter();
            sortParam.ParameterName = "@sortby";
            sortParam.SqlDbType = SqlDbType.VarChar;
            sortParam.Size = 500;
            sortParam.Value = sortby.Trim();
            sortParam.Direction = ParameterDirection.InputOutput;

            SqlParameter dirParam = new SqlParameter();
            dirParam.ParameterName = "@direction";
            dirParam.SqlDbType = SqlDbType.VarChar;
            dirParam.Size = 500;
            dirParam.Value = direction.Trim();
            dirParam.Direction = ParameterDirection.InputOutput;

            IEnumerable<InterestedParty> interestedParties = DatabaseUtility.ExecuteReader<InterestedParty>(DatabaseUtility.DataWarehouseConnectionString,
                            "cp_GetDealViewInterestedParties",
                            CommandType.StoredProcedure,
                            new SqlParameter[] { new SqlParameter("@opportunityid", opportunityId),
                            new SqlParameter("@fromIndex", fromIndex),
                            new SqlParameter("@toindex", toindex),
                            new SqlParameter("@ipstatus", IPStatus),
                            new SqlParameter("@jllbroker", JllBroker),
                            new SqlParameter("@IPNameSearch", ipsearch),
                            new SqlParameter("@domainname", username),
                            sortParam,
                            dirParam,
                            new SqlParameter("@offerType", offerType),
                            outParam},
                            interestedPartyMapper.Mapper);
            totalRecords = Convert.ToInt32(outParam.Value);
            sortby = sortParam.Value.ToString();
            direction = dirParam.Value.ToString();
            return interestedParties.ToList();
}

存储过程参数如下:

ALTER PROCEDURE [dbo].[cp_GetDealViewInterestedParties] 
 @opportunityid uniqueidentifier, 
 @ipstatus  nvarchar(250) = null,
 @jllbroker uniqueidentifier = null, 
 @fromindex int = 1,
 @toindex int = 40,  
 @IPNameSearch  nvarchar(255) = null,
 @offerType int = 0,
 @sortby nvarchar(255) OUTPUT,
 @direction varchar(4) OUTPUT,
 @domainname varchar(255) = null,
 @TotalRecords int OUTPUT
 with recompile
AS
BEGIN
set nocount on
    DECLARE @defaultSortBy nvarchar(255) = 'ModifiedOn'
    DECLARE @defaultDirection varchar(4) = 'desc'
    DECLARE @userPreferenceLookupResults nvarchar(255)

我收到以下错误:

形式参数“@sortby”未声明为 OUTPUT 参数,而是在请求的输出中传递的实际参数。

感谢任何想法和想法

【问题讨论】:

    标签: sql-server-2008


    【解决方案1】:

    在您的代码中,您将“@sortby”定义为 varchar,而您的存储过程需要 nvarchar 类型。更改此设置是否可以解决问题?

            SqlParameter sortParam = new SqlParameter();
            sortParam.ParameterName = "@sortby";
            sortParam.SqlDbType = SqlDbType.VarChar; //<---- should be nvarchar?
            sortParam.Size = 500;
            sortParam.Value = sortby.Trim();
            sortParam.Direction = ParameterDirection.InputOutput;
    
    
    
    -- sql def: @sortby nvarchar(255) OUTPUT,
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      相关资源
      最近更新 更多