Is it possible to query records obtained from extended stored procedures from TSQL in C #?
So this is the code I tried, in C #, that didn't give me the result I want.
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 fetch data from these logs (which is retrieved from SQL Server using this stored procedure) and it seems that when I use dataReader, there are no records and if I use dataset with adapter data, there are no tables in the dataset either / records. This information is very important for the request.
Is there a way that I can query the SQL Server error logs without resorting to stored procedures?
ANOTHER UPDATE:
Parameters for these extended stored procedures:
-
The value of the error log file that 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: The string you want to find
-
Search string 2: The string you want to search to further refine the results
-
Search since launch
-
Search until the end
-
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 data, I could use this following extract, but it would be too expanded and 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
source to share
You cannot use AddWithValue
for dates.
If the dates are empty, you need to pass null as a value, not an empty string. They have completely different meanings.
To check, open Management Studio and do the following:
exec sys.xp_readerrorlog 0,1, '', '', '', ''
This will have zero results. However, if you do this:
exec sys.xp_readerrorlog 0,1, '', '', null, null
You will receive many entries.
By the way, your update is still wrong. The dataset code you have will never do anything. Change it to:
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
Notice the fill command ...
source to share