gpt4 book ai didi

c# - 是否可以在 C# 中查询从 TSQL 的扩展存储过程中获取的记录?

转载 作者:行者123 更新时间:2023-11-30 17:13:26 24 4
gpt4 key购买 nike

所以这是我在 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 代理日志

  • Search string 1:要搜索的字符串一

  • Search string 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

最佳答案

您不能对日期使用 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

注意填充命令...

关于c# - 是否可以在 C# 中查询从 TSQL 的扩展存储过程中获取的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9722990/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com