简体   繁体   English

如何将存储过程中的值从SQL返回到C#

[英]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.

相关问题 C#和SQL Server:如何通过使用C#调用存储过程来从SQL表中获取值? - C# and SQL Server: How do I get values from my SQL Table, by calling my stored procedure with C#? 如何在数据库上下文中运行没有C#返回值的存储过程? - How can I run a stored procedure that has no return values from C# with my db context? 如何从ASP C#中的标量存储过程获取返回值? - How do I get the return value from a scalar stored procedure in ASP C#? 从C#中的存储过程获取返回值(登录过程) - Get return values from a stored procedure in c# (login process) 在C#中从存储过程返回多个值 - Return multiple values from Stored Procedure in C# 如何从SQL Server存储过程接收一个整数作为C#的返回值? - How to receive an integer as a return value into C# from SQL Server stored procedure? 如何让C#Query组件识别从sql存储过程中的临时表返回的数据列 - How do I get the C# Query component to recognise columns returned data from a temporary table in a sql stored procedure 从SQL在C#中运行存储过程 - Run a Stored procedure in c# from SQL C#:如何从存储过程在ComboBox中添加2个值 - C#: how to add 2 values in ComboBox from stored procedure 如何将数据表从 C# 传递到 SQL 服务器存储过程 - How to pass datatable from C# to SQL Server stored procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM