[英]How do I return values from my stored procedure from SQL to c#
So I am storing a value which is the student reference number which I have done, not I need to return the value from my ID and my DateTime in textbox2 and textbox 3. Whilst inputting and executing the procedure. 因此,我要存储一个已经完成的学生参考号的值,而不是需要在文本框2和文本框3中从我的ID和DateTime返回该值。
using (SqlConnection con = new *****))
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "Exams_BagCheck_ScanIn";
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@StudentReference", SqlDbType.VarChar).Value = textBox1.Text;
cmd.Parameters.AddWithValue("@ID", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
con.Open();
cmd.ExecuteNonQuery();
The procedure 步骤
USE [IFCustom]
GO
DECLARE @return_value Int
EXEC @return_value = [dbo].[Exams_BagCheck_ScanIn]
@StudentReference = N'1'
SELECT @return_value as 'Return Value'
GO
Your procedure returns nothing, you need to remove: 您的过程未返回任何内容,您需要删除:
SELECT @return_value as 'Return Value'
And instead: 相反:
RETURN @return_value
(If you wanted to select
then .ExecuteScalar()
would return the value) (如果要
select
那么.ExecuteScalar()
将返回该值)
If you just returning a single value then you can use 如果您只返回一个值,则可以使用
this: 这个:
string some_value = cmd.ExecuteScalar().ToString(); // if it is string
OR 要么
Object some_value = cmd.ExecuteScalar(); // convert it to its repective type.
in place of 代替
cmd.ExecuteNonQuery();
// some_value
will contain the value you selected as SELECT @return_value
in procedure. //
some_value
将包含您在过程中选择为SELECT @return_value
的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.