So this is the code I have tried, in C#, which failed to give me the result I needed.
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));
}
Basically, I need to extract data from these logs (which gets pulled from the SQL Server through this stored procedure), and it seems that When I use a dataReader, there are no records, and if I use a dataset with data adapter, there are also no tables/records in the dataset. This information is critical for me to query.
Is there a way that I can still query the SQL Server error logs without having to resort to stored procedures?
ANOTHER UPDATE:
The parameters for this extended stored procedures are:
-
Value of error log file you want to read: 0 = current, 1 = Archive, 2 = etc…
-
Log file type: 1 or NULL = error log, 2 = SQL Agent log
-
Search string 1: String one you want to search for
-
Search string 2: String two you want to search for to further refine
the results -
Search from start time
-
Search to end time
-
Sort order for results: N’asc’ = ascending, N’desc’ = descending
Another method I tried
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 i was allowed to use stored procedures to query the data, I could have used this following extract, but it would have been deployed too much and be a pain to maintain and decommission
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
You can’t use
AddWithValuefor the dates.If the dates are blank, then you need to pass null as the value, not an empty string. Those have completely different meanings.
To test, open Management Studio and execute the following:
That will have zero results. However if you do this:
You will get back a lot of records.
BTW, your update is still wrong. The dataset code you have will never do anything. Change it to:
Note the fill command…