存储过程的返回值仅在 ASP.NET 中获取第一个字符
从存储过程获取返回值时,仅返回第一个字符,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
确保您确实像这样使用它:
(N)VARCHAR 列的默认长度是 1。
Make sure you really use it like this:
The default length for (N)VARCHAR columns is 1.
根据 MSDN:
因此指定
out
参数的大小非常重要。According to MSDN:
So it's important to specify the size for
out
parameters.我找到了答案:
I find the answer with:
这应该可以解决这个问题!
This should address the issue!