从存储过程获取结果然后传递给WPF文本框(Get result from Stored Procedure then pass to WPF TextBox)

编程入门 行业动态 更新时间:2024-10-09 11:21:44
存储过程获取结果然后传递给WPF文本框(Get result from Stored Procedure then pass to WPF TextBox)

我有这个存储过程执行得很好:

ALTER PROCEDURE [dbo].[SP_GENERATETICKET] @RESULT AS VARCHAR(255) OUTPUT AS BEGIN DECLARE @D AS DATETIME = GETDATE() DECLARE @LASTTICKET AS VARCHAR(50) ; DECLARE @NUM AS VARCHAR(50); SET @LASTTICKET=(SELECT TOP 1 TICKETNO FROM tblTicket WHERE MONTH(ENTRYVEHICLE ) = MONTH(@D ) ORDER BY TICKETNO DESC); IF (CONVERT(VARCHAR(10),YEAR(@D),110) <> CONVERT(VARCHAR(10),YEAR(GETDATE()),110))--IF YEAR IS DIFFERENT, RESET SERIES BEGIN SET @NUM = '1' END ELSE BEGIN IF ISNULL(@LASTTICKET,'1') = '1'--IF MONTH IS DIFFERENT, RESET SERIES BEGIN SET @NUM ='1' END ELSE BEGIN SET @NUM = CAST(CAST (RIGHT(@LASTTICKET,5) AS INT) + 1 AS VARCHAR) END END SET @RESULT = RIGHT(CONVERT(VARCHAR(10),@D,101),2) + '-' + LEFT(CONVERT(VARCHAR(10),@D,101),2) + '-' + RIGHT('0000'+CAST(@NUM AS VARCHAR),5) SELECT @RESULT END

我有这个代码,但它返回null

SqlConnection con = new SqlConnection(gsql.connectionString); SqlCommand command = new SqlCommand("SP_GENERATETICKET", con); try { con.Open(); command.CommandType = CommandType.StoredProcedure; SqlParameter result = command.Parameters.Add("@RESULT", SqlDbType.VarChar); result.Direction = ParameterDirection.Output; txtTicket.Text = (string)command.Parameters["@RESULT"].Value; } catch(Exception ex) { MessageBox.Show(ex.Message); }

我该怎么办?

I have this stored procedure that executes fine:

ALTER PROCEDURE [dbo].[SP_GENERATETICKET] @RESULT AS VARCHAR(255) OUTPUT AS BEGIN DECLARE @D AS DATETIME = GETDATE() DECLARE @LASTTICKET AS VARCHAR(50) ; DECLARE @NUM AS VARCHAR(50); SET @LASTTICKET=(SELECT TOP 1 TICKETNO FROM tblTicket WHERE MONTH(ENTRYVEHICLE ) = MONTH(@D ) ORDER BY TICKETNO DESC); IF (CONVERT(VARCHAR(10),YEAR(@D),110) <> CONVERT(VARCHAR(10),YEAR(GETDATE()),110))--IF YEAR IS DIFFERENT, RESET SERIES BEGIN SET @NUM = '1' END ELSE BEGIN IF ISNULL(@LASTTICKET,'1') = '1'--IF MONTH IS DIFFERENT, RESET SERIES BEGIN SET @NUM ='1' END ELSE BEGIN SET @NUM = CAST(CAST (RIGHT(@LASTTICKET,5) AS INT) + 1 AS VARCHAR) END END SET @RESULT = RIGHT(CONVERT(VARCHAR(10),@D,101),2) + '-' + LEFT(CONVERT(VARCHAR(10),@D,101),2) + '-' + RIGHT('0000'+CAST(@NUM AS VARCHAR),5) SELECT @RESULT END

I have this Code but it returns null

SqlConnection con = new SqlConnection(gsql.connectionString); SqlCommand command = new SqlCommand("SP_GENERATETICKET", con); try { con.Open(); command.CommandType = CommandType.StoredProcedure; SqlParameter result = command.Parameters.Add("@RESULT", SqlDbType.VarChar); result.Direction = ParameterDirection.Output; txtTicket.Text = (string)command.Parameters["@RESULT"].Value; } catch(Exception ex) { MessageBox.Show(ex.Message); }

What should I do?

最满意答案

有问题,你忘了执行命令

command.ExecuteNonQuery()

不执行命令你想要返回结果。

你也忘了为你的varchar参数添加大小,所以添加东西线

result.Size = 8000;

在下面的代码中使用udpated


你的代码将会是,你需要使用using来为你处理事情

using(SqlConnection con = new SqlConnection(gsql.connectionString)) { using(SqlCommand command = new SqlCommand("SP_GENERATETICKET", con)) { try { con.Open(); command.CommandType = CommandType.StoredProcedure; SqlParameter result = command.Parameters.Add("@RESULT", SqlDbType.VarChar); result.Size = 8000;//add parameter size result.Direction = ParameterDirection.Output; command.ExecuteNonQuery();//this line need to be added txtTicket.Text = (string)command.Parameters["@RESULT"].Value; } catch(Exception ex) { MessageBox.Show(ex.Message); } } }

there problem is there , you forgot to execute command

command.ExecuteNonQuery()

without executing command you want get result back.

also you forgot to add size for you varchar paramter , so add thing line

result.Size = 8000;

udpated in below code


you code will be , you need to use using to dispose things for you

using(SqlConnection con = new SqlConnection(gsql.connectionString)) { using(SqlCommand command = new SqlCommand("SP_GENERATETICKET", con)) { try { con.Open(); command.CommandType = CommandType.StoredProcedure; SqlParameter result = command.Parameters.Add("@RESULT", SqlDbType.VarChar); result.Size = 8000;//add parameter size result.Direction = ParameterDirection.Output; command.ExecuteNonQuery();//this line need to be added txtTicket.Text = (string)command.Parameters["@RESULT"].Value; } catch(Exception ex) { MessageBox.Show(ex.Message); } } }

更多推荐

本文发布于:2023-08-03 19:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1398098.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:存储过程   文本框   WPF   result   TextBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!