c# - ASP.NET SQL Server 存储过程返回消息

标签 c# asp.net sql-server stored-procedures

我有一个不需要任何参数的存储过程,它返回值 0 和消息:

(94 row(s) affected)

(1 row(s) affected)

我的问题是如何获取消息:

(94 row(s) affected)

(1 row(s) affected)

这是调用存储过程的 .NET 方法:

public List<MessageClass> ChequesToUpdate()
{
    message = new List<MessageClass>();

    MessageClass item = new MessageClass();

    try
    {
        using (connection = new SqlConnection(connectionString))
        {
            connection.Open();

            using (SqlCommand command = new SqlCommand("MyStoredProcedure", connection))
            {
                command.CommandType = CommandType.StoredProcedure;

                command.ExecuteNonQuery();

                item.message = "message";
            }
        }
    }
    catch (Exception e)
    {
        item.message = e.Message;
    }
    finally
    {
        connection.Close();
    }

    message.Add(item);

    return message;
}

我希望将消息放入 item.message,我该如何实现?

最佳答案

ExecuteNonQuery 返回受影响行的总数。因此,如果您只想要总行数,那么您可以使用以下语句简单地获取它:

var x = command.ExecuteNonQuery();

否则,您必须在存储过程中使用用户定义的 RAISERROR 消息,并从 C# connection.InfoMessage 事件中捕获它。我已经设置了一个测试环境并对其进行了测试。我创建了一个表并插入了一些数据来检查我的 SQL 和 C# 代码。请检查以下 SQL 和 C# 代码。

SQL:

Create Table psl_table
(
    [values] NVarChar(MAX)
)

Insert Into psl_table Values('a')
Insert Into psl_table Values('a')
Insert Into psl_table Values('a')
Insert Into psl_table Values('b')
Insert Into psl_table Values('b')
Insert Into psl_table Values('b')
Insert Into psl_table Values('b')
Insert Into psl_table Values('b')
Insert Into psl_table Values('b')
Insert Into psl_table Values('b')

Create Proc MyStoredProcedure
    As
Begin
    -- Declare a variable for Message
    Declare @Msg NVarChar(MAX)

    -- 1st SQL Statement
    Update psl_table Set [Values]='a' Where [Values]!='a'

    -- Generate the message and print that can get from C#
    Set @Msg = '(' + Convert(NVarChar,@@RowCount) + ' row(s) affected)'
    RAISERROR( @Msg, 0, 1 ) WITH NOWAIT

    -- 2nd SQL Statement
    Update psl_table Set [Values]='a'

    -- Generate the message and print that can get from C#
    Set @Msg = '(' + Convert(NVarChar,@@RowCount) + ' row(s) affected)'
    RAISERROR( @Msg, 0, 1 ) WITH NOWAIT
End

在这个 SQL 中,我声明了一个变量 @Msg 来存储消息和内置函数 RAISERROR 来抛出消息。

C#代码:

public List<MessageClass> ChequesToUpdate()
{
    message = new List<MessageClass>();

    MessageClass item = new MessageClass();

    try
    {
        using (connection = new SqlConnection(connectionString))
        {
            connection.Open();

            connection.InfoMessage += delegate (object sender, SqlInfoMessageEventArgs e)
            {
                item.message = e.Message;
            };

            using (SqlCommand command = new SqlCommand("MyStoredProcedure", connection))
            {
                command.CommandType = CommandType.StoredProcedure;

                command.ExecuteNonQuery();
            }
        }
    }
    catch (Exception e)
    {
        item.message = e.Message;
    }
    finally
    {
        connection.Close();
    }

    message.Add(item);

    return message;
}

我已经修改了您的代码以获得所需的输出。我使用 connection.InfoMessage 事件来捕获从 SQL 抛出的消息。

出于测试目的,我在控制台中打印了输出。

输出:

Output

关于c# - ASP.NET SQL Server 存储过程返回消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42542356/

相关文章:

C# 使用函数将值添加到列表

c# - ASP.NET 按 ID 终止 session

sql - 如何根据 SQL Server 2005 中的条件返回值集?

sql - 在 SSMS 中执行最后一个查询的快捷方式

asp.net - 如何使用 jQuery 捕获 ASP.NET GridView 中的 'Update' 点击事件

sql-server - SQL Server与Docker和Entity Framework的连接

c# - 从字符串中读取 double 值

c# - 从 C# 休眠计算机

c# - 查询不会从 npgsql 运行,而是在 postgresql pgAdmin 上运行

c# - GridView 取一行