是否可以从C#中的TSQL查询从扩展存储过程中获得的记录

本文关键字:过程中 存储过程 存储 记录 查询 中的 是否 TSQL 扩展 | 更新日期: 2023-09-27 18:27:50

所以这是我在C#中尝试的代码,它没有给我所需的结果。

SqlCommand comm = new SqlCommand("exec sys.xp_readerrorlog 0,1,'','',@StartDate,@EndDate,N'Desc'");, conn);
comm.Parameters.AddWithValue("@StartDate", "");
comm.Parameters.AddWithValue("@EndDate", "");
SqlDataReader dr = comm.ExecuteReader();
while (dr.Read())
{
    Console.WriteLine(dr.GetString(0));
}

基本上,我需要从这些日志中提取数据(通过这个存储过程从SQL Server中提取),而且当我使用dataReader时,似乎没有记录,如果我使用带有数据适配器的数据集,那么数据集中也没有表/记录。这些信息对我的查询至关重要。

有没有一种方法可以让我在不使用存储过程的情况下查询SQL Server错误日志?

另一个更新:

此扩展存储过程的参数为:

  • 您要读取的错误日志文件的值:0=当前,1=存档,2=等…

  • 日志文件类型:1或NULL=错误日志,2=SQL代理日志

  • 搜索字符串1:要搜索的字符串

  • 搜索字符串2:要搜索以进一步细化的字符串2结果

  • 从开始搜索

  • 搜索结束时间

  • 结果排序顺序:N’asc’=升序,N’desc’=降序

我试过的另一种方法

SqlCommand comm = new SqlCommand(@"exec sys.xp_readerrorlog 0,1,'','',null,null,N'Desc'", conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
Console.WriteLine(ds.Tables.Count); //0 returned: no data in dataset

如果允许我使用存储过程来查询数据,我本可以使用下面的摘录,但它会被部署得太多,维护和停用会很痛苦

    IF (EXISTS( SELECT * FROM sys.procedures where name = 'writelogs' ))
BEGIN
    DROP PROCEDURE Writelogs;
END
GO
CREATE PROCEDURE WriteLogs @Servername varchar(40),@InstanceName varchar(40),@Pattern varchar(max),@ParamBeginDate varchar(40), @ParamEndDate varchar(40) AS
BEGIN 
    DECLARE @BeginDate DateTime
    DECLARE @EndDate DateTime
    DECLARE @NextQueryID int
    
    --First we have to convert the timestamps EndDate and BeginDate to something usable
    IF (@ParamBeginDate = 'Beginning')
    BEGIN
        SET @BeginDate = null;  --null will cause sys.xp_readerrorlog to read from beginning
    END
    ELSE IF (@ParamBeginDate = 'Last')
    BEGIN
        SELECT TOP 1 @BeginDate = L.TimeLogged FROM LogTable L ORDER BY L.TimeLogged Desc
    END
    ELSE
    BEGIN
        BEGIN TRY   
            SET @BeginDate = CAST(@ParamBeginDate AS DATETIME);
        END TRY
        BEGIN CATCH
            SET @BeginDate = null;
        END CATCH
    END
    IF (@ParamEndDate = 'Now')
    BEGIN
        SET @EndDate = GETDATE();  --null will cause sys.xp_readerrorlog to read till now
    END
    ELSE
    BEGIN
        BEGIN TRY   
            SET @EndDate = CAST(@ParamEndDate AS DATETIME);
        END TRY
        BEGIN CATCH
            SET @EndDate = GETDATE();
        END CATCH
    END
    
    --Temporary Table to store the logs in the format it is originally written in
    CREATE TABLE TMP
                (LogDate DateTime2
                ,Processinfo varchar(40)
                ,[Text] varchar(max))
    --truncate the milliseconds (else ALL records will be retrieved)
    SET @EndDate= dateadd(millisecond, -datepart(millisecond, @EndDate),@EndDate);
    SET @BeginDate= dateadd(millisecond, -datepart(millisecond, @BeginDate),@BeginDate);
    
    INSERT INTO TMP exec sys.xp_readerrorlog 0,1,'','',@BeginDate,@EndDate,N'DESC';
    SELECT TOP 1 L.TimeLogged FROM LogTable L ORDER BY L.Timelogged desc
    INSERT INTO LogTable
    SELECT @Servername,@InstanceName,T.[text],T.LogDate,GETDATE(),0,0,null,@NextQueryID FROM TMP t WHERE PATINDEX(@Pattern,t.[Text]) > 0;
    DROP TABLE TMP;
END

是否可以从C#中的TSQL查询从扩展存储过程中获得的记录

日期不能使用AddWithValue

如果日期为空,则需要传递null作为值,而不是空字符串。这些有着完全不同的含义。

要进行测试,请打开Management Studio并执行以下操作:

exec sys.xp_readerrorlog 0,1, '', '', '', ''

这将不会产生任何结果。然而,如果你这样做:

exec sys.xp_readerrorlog 0,1, '', '', null, null

你会得到很多唱片。


顺便说一句,你的更新仍然是错误的。你拥有的数据集代码永远不会做任何事情。更改为:

SqlCommand comm = new SqlCommand(@"exec sys.xp_readerrorlog 0,1,'','',null,null,N'Desc'", conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds, "sometablename");
Console.WriteLine(ds.Tables.Count); //0 returned: no data in dataset

注意填充命令。。。