是否可以从C#中的TSQL查询从扩展存储过程获得的记录?
这就是我用C#编写的代码,它没有给出我需要的结果是否可以从C#中的TSQL查询从扩展存储过程获得的记录?,c#,tsql,C#,Tsql,这就是我用C#编写的代码,它没有给出我需要的结果 SqlCommand comm = new SqlCommand("exec sys.xp_readerrorlog 0,1,'','',@StartDate,@EndDate,N'Desc'");, conn); comm.Parameters.AddWithValue("@StartDate", ""); comm.Parameters.AddWithValue("@End
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时,似乎没有记录,如果我使用带有数据适配器的dataset,则dataset中也没有表/记录。这些信息对我来说至关重要
有没有一种方法可以让我不用借助存储过程,仍然可以查询SQL Server错误日志
另一更新:
此扩展存储过程的参数为:
- 要读取的错误日志文件的值:0=当前,1=存档,2=等
- 日志文件类型:1或NULL=错误日志,2=SQL代理日志
- 搜索字符串1:要搜索的字符串
- 搜索字符串2:要搜索以进一步细化的字符串2 结果
- 从开始时间开始搜索
- 搜索结束时间
- 结果的排序顺序:N'asc'=升序,N'desc'=降序
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
注意fill命令…仍然没有返回任何记录prob is,这不是我需要它的地方,我需要用C#查询,然后处理这些数据(在提取数据之后)。制作后台监控应用程序我的连接字符串绝对不会像well@EonRustedduPlessis:使用SQL探查器查看查询的确切内容。您的数据集代码不会返回任何内容。查看我的更新。
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