2011-03-07 59 views
8

當從一個存儲過程中得到一個返回值,它只返回第一個字符,從存儲過程返回值獲得的第一個字符只能在ASP.NET

Exec sp_Auto_Gen_TTBDBatNo 'TT', ''在SQL服務器 獲得了整個串,但在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; 
    } 
} 

回答

10

確保您真正使用它是這樣的:

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

的默認長度(N)VARCHAR列是1.

1

我找到了答案:

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

50是有點矯枉過正,如果在存儲過程中你定義長度爲8。 – 2011-03-07 11:36:36

2

根據MSDN:

對於具有可變長度型(nvarchar的,例如)的輸出參數,該參數的大小定義了緩衝器保持輸出參數的大小。輸出參數可以截斷爲Size指定的大小。

因此,指定out參數的大小很重要。

相關問題