首页 > 解决方案 > 使用 SqlCommand 在存储过程中获取底层执行查询

问题描述

假设我有一个存储过程(示例如下)

CREATE OR ALTER PROCEDURE test
AS
BEGIN
    SELECT * FROM sys.tables;
    WAITFOR DELAY '01:00'
    SELECT * FROM sys.tables
END
GO;

我有一个 SQLCommand 的 ac# 调用,它运行一个存储过程,如下所示

using (var connection = new SqlConnection(connectionString)){
        connection.Open();
        using(var command = new SqlCommand("test", connection)){
            command.CommandType = CommandType.StoredProcedure;
            command.ExecuteNonQuery();
        }
    }

有没有办法让我获得当前在存储过程中执行的底层 sql 语句?

我想要的是能够记录“SELECT * FROM sys.tables”、“WAITFOR DELAY '01:00'”等。

标签: c#sqlcommand

解决方案


推荐阅读