存储过程的返回值仅在ASP.NET中获取第一个字符

Jo0*_*0o0 8 .net c# sql-server asp.net ado.net

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

Exec sp_Auto_Gen_TTBDBatNo 'TT', '' 在SQL Server中获取整个字符串,但在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
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Jar*_*dek 10

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

@Result NVARCHAR(8) OUTPUT
SqlParameter resultValue = new SqlParameter("@Result", SqlDbType.NVarChar, 8);
Run Code Online (Sandbox Code Playgroud)

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