当前位置:首页 > 行业动态 > 正文

c# 存储过程输入输出参数

在C#中,存储过程的输入输出参数可以通过 SqlParameter类来定义和传递。

C# 存储过程输入输出参数详解

在C#中,使用ADO.NET与SQL Server交互时,经常需要调用存储过程(Stored Procedures),存储过程可以接受输入参数和返回输出参数,这使得它们非常灵活且强大,本文将详细介绍如何在C#中处理存储过程的输入输出参数。

创建存储过程

我们需要在SQL Server中创建一个示例存储过程,假设我们有一个存储过程,它接受一个整数作为输入参数,并返回一个计算后的整数作为输出参数。

CREATE PROCEDURE GetSquare
    @InputInt INT,
    @OutputInt INT OUTPUT
AS
BEGIN
    SET @OutputInt = @InputInt  @InputInt;
END

C# 中调用存储过程

我们在C#代码中调用这个存储过程,以下是一个完整的示例,展示了如何传递输入参数并接收输出参数。

using System;
using System.Data;
using System.Data.SqlClient;
class Program
{
    static void Main()
    {
        // 定义连接字符串
        string connectionString = "Your_Connection_String_Here";
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            // 定义输入参数和输出参数
            SqlParameter inputParam = new SqlParameter("@InputInt", SqlDbType.Int);
            inputParam.Value = 5; // 设置输入参数的值
            SqlParameter outputParam = new SqlParameter("@OutputInt", SqlDbType.Int);
            outputParam.Direction = ParameterDirection.Output; // 设置参数方向为输出
            try
            {
                // 打开连接
                connection.Open();
                // 创建命令对象并指定存储过程名称
                SqlCommand command = new SqlCommand("GetSquare", connection);
                command.CommandType = CommandType.StoredProcedure;
                // 添加参数到命令对象
                command.Parameters.Add(inputParam);
                command.Parameters.Add(outputParam);
                // 执行命令
                command.ExecuteNonQuery();
                // 获取输出参数的值
                int result = (int)command.Parameters["@OutputInt"].Value;
                Console.WriteLine($"The square of {inputParam.Value} is {result}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
        }
    }
}

解释代码

连接字符串:替换"Your_Connection_String_Here"为你的实际数据库连接字符串。

SqlParameter:用于定义输入和输出参数。inputParam是输入参数,而outputParam是输出参数,注意,输出参数的方向需要设置为ParameterDirection.Output

SqlCommand:用于执行存储过程,通过设置CommandTypeCommandType.StoredProcedure来指定这是一个存储过程。

ExecuteNonQuery:执行存储过程,由于存储过程不返回行集,因此使用ExecuteNonQuery方法。

获取输出参数值:通过访问command.Parameters["@OutputInt"].Value来获取输出参数的值。

步骤 描述 代码示例
创建存储过程 在SQL Server中定义存储过程,包含输入和输出参数 CREATE PROCEDURE GetSquare @InputInt INT, @OutputInt INT OUTPUT AS BEGIN SET @OutputInt = @InputInt @InputInt; END
定义输入参数 在C#中创建SqlParameter对象,设置参数名称、类型和值 SqlParameter inputParam = new SqlParameter("@InputInt", SqlDbType.Int); inputParam.Value = 5;
定义输出参数 在C#中创建SqlParameter对象,设置参数名称、类型和方向 SqlParameter outputParam = new SqlParameter("@OutputInt", SqlDbType.Int); outputParam.Direction = ParameterDirection.Output;
执行存储过程 使用SqlCommand对象执行存储过程,并传递参数 SqlCommand command = new SqlCommand("GetSquare", connection); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(inputParam); command.Parameters.Add(outputParam); command.ExecuteNonQuery();
获取输出参数值 SqlParameter对象中读取输出参数的值 int result = (int)command.Parameters["@OutputInt"].Value;

相关问答FAQs

Q1: 如果存储过程有多个输入和输出参数,如何处理?

A1: 你只需为每个参数创建相应的SqlParameter对象,并添加到SqlCommand对象的Parameters集合中。

SqlParameter inputParam1 = new SqlParameter("@Param1", SqlDbType.Int);
inputParam1.Value = 10;
SqlParameter inputParam2 = new SqlParameter("@Param2", SqlDbType.VarChar);
inputParam2.Value = "Hello";
SqlParameter outputParam = new SqlParameter("@Result", SqlDbType.Int);
outputParam.Direction = ParameterDirection.Output;
command.Parameters.Add(inputParam1);
command.Parameters.Add(inputParam2);
command.Parameters.Add(outputParam);

Q2: 如果存储过程返回结果集,如何处理?

A2: 如果存储过程返回结果集,可以使用SqlDataReader来读取数据。

SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    Console.WriteLine($"Column1: {reader["Column1"]}, Column2: {reader["Column2"]}");
}
reader.Close();

通过以上步骤和示例代码,你可以在C#中轻松地处理存储过程的输入和输出参数,希望这些信息对你有所帮助!

0