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

发布于 2024-10-20 12:42:33 字数 2415 浏览 2 评论 0原文

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

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;
    }
}

When getting a return value from a stored procedure, it only returns the first character,

Exec sp_Auto_Gen_TTBDBatNo 'TT', '' in SQL Server gets the whole string, but in ASP.NET it gets the first character.

How do I get the whole string value?

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# code:

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

闻呓 2024-10-27 12:42:33

确保您确实像这样使用它:

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

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

Make sure you really use it like this:

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

The default length for (N)VARCHAR columns is 1.

傾城如夢未必闌珊 2024-10-27 12:42:33

根据 MSDN:

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

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

According to MSDN:

For output parameters with a variable length type (nvarchar, for example), the size of the parameter defines the size of the buffer holding the output parameter. The output parameter can be truncated to a size specified with Size.

So it's important to specify the size for out parameters.

你没皮卡萌 2024-10-27 12:42:33

我找到了答案:

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

I find the answer with:

command.Parameters["@Result"].size = 50
病毒体 2024-10-27 12:42:33
        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);

这应该可以解决这个问题!

        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);

This should address the issue!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文