【问题标题】:The return value from a stored procedure gets the first character only in ASP.NET存储过程的返回值仅在 ASP.NET 中获取第一个字符
【发布时间】:2011-03-07 08:46:22
【问题描述】:

从存储过程中获取返回值时,只返回第一个字符,

SQL Server 中的Exec sp_Auto_Gen_TTBDBatNo 'TT', '' 获取整个字符串,但在 ASP.NET 中获取第一个字符。

如何获取整个字符串值?

CREATE PROC sp_Auto_Gen_TTBDBatNo
      @Prefix nvarchar(2),
      @Result nvarchar(8) output
AS
BEGIN
    DECLARE @LastValue int


    -- CompanyCode = @CompanyCode AND BankCode = @BankCode AND AccountCode = @AccountCode

    SET NOCOUNT ON
    If @Prefix = 'BD'
        SELECT @LastValue = MAX(RIGHT(RTRIM(ISNULL(BatchNo, '')),2)) from dbo.Cheque_IssueRecord_Secretary_Review_BD WHERE ISNUMERIC(RIGHT(RTRIM(BatchNo),2))= 1 AND LEN(RIGHT(RTRIM(BatchNo),2)) = 2
    ELSE
        SELECT @LastValue = MAX(RIGHT(RTRIM(ISNULL(BatchNo, '')),2)) from dbo.Cheque_IssueRecord_Secretary_Review_TT WHERE ISNUMERIC(RIGHT(RTRIM(BatchNo),2))= 1 AND LEN(RIGHT(RTRIM(BatchNo),2)) = 2

    SET NOCOUNT OFF
    set @Result = @Prefix + RIGHT(RTRIM(STR(year(getdate()))),2)+RIGHT('0'+LTRIM(RTRIM(STR(month(getdate())))),2) + RIGHT('0'+LTRIM(RTRIM(STR(ISNULL(@LastValue,0)+1))),2)

    print @Result
END

C#代码:

string tAuto_Batch = "";

SqlTransaction trans = null;
using (SqlConnection connection = new SqlConnection(_connectionString))
{
    try
    {
        SqlCommand command = new SqlCommand("sp_Auto_Gen_TTBDBatNo", connection);
        command.CommandType = CommandType.StoredProcedure;

        command.Parameters.Add(new SqlParameter("@Prefix", "TT"));
        //command.Parameters.Add(new SqlParameter("@CompanyCode", cheque.Voucherbatchno));
        //command.Parameters.Add(new SqlParameter("@BankCode", cheque.Voucherno));
        //command.Parameters.Add(new SqlParameter("@AccountCode", cheque.Voucherno));

        SqlParameter ResultValue = new SqlParameter("@Result", tAuto_Batch);
        ResultValue.Direction = ParameterDirection.Output;

        command.Parameters.Add(ResultValue);

        connection.Open();
        trans = connection.BeginTransaction();
        command.Transaction = trans;
        command.Connection = connection;

        command.ExecuteNonQuery();
        trans.Commit();

        tAuto_Batch = command.Parameters["@Result"].Value.ToString();

        command.Dispose();
        trans.Dispose();
        connection.Close();

    }
    catch (Exception ex)
    {
        connection.Close();
        Error_Label.Text = Error_Label.Text + "sp_Auto_Gen_TTBDBatNo error " + ex.Message;
    }
}

【问题讨论】:

    标签: c# asp.net .net sql-server ado.net


    【解决方案1】:

    确保你真的像这样使用它:

    @Result NVARCHAR(8) OUTPUT
    SqlParameter resultValue = new SqlParameter("@Result", SqlDbType.NVarChar, 8);
    

    (N)VARCHAR 列的默认长度为 1。

    【讨论】:

      【解决方案2】:

      根据 MSDN:

      对于具有可变长度类型(例如 nvarchar)的输出参数,参数的大小定义了保存输出参数的缓冲区的大小。可以将输出参数截断为 Size 指定的大小。

      因此指定out 参数的大小很重要。

      【讨论】:

        【解决方案3】:

        我找到了答案:

        command.Parameters["@Result"].size = 50
        

        【讨论】:

        • 50 有点矫枉过正,如果在存储过程中将长度定义为 8。
        【解决方案4】:
                SqlParameter outParam = new SqlParameter();
                outParam.SqlDbType = SqlDbType.NVarChar;
                outParam.Size = 50;
                outParam.ParameterName = "@checkid";
                outParam.Value = "";
                outParam.Direction = System.Data.ParameterDirection.Output;
                command.Parameters.Add(outParam);
        

        这应该可以解决问题!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-10-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-22
          • 1970-01-01
          • 2019-01-04
          相关资源
          最近更新 更多