G
Guest
I'm trying to copy records from the Event Logs to a databse. I can read the
database & the event logs but hit a problem when trying to create a NewRow.
Follows is the code - scroll down I've indicated the line in question.
Thanks in advance for assistance.
BigSam
Imports System
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form1
Protected objDataAdapter As New SqlDataAdapter
Protected conn As New SqlConnection
Protected objCmd As New SqlCommand
Protected objDataSet As New DataSet
Protected objDataTable As New DataTable
Protected moReturn As Management.ManagementObjectCollection
Protected moSearch As New Management.ManagementObjectSearcher
Protected mo As New Management.ManagementObject
Protected strComputer As String = "Samsbox"
Protected strSelect As String
Protected strReturn As String
Protected strSearch As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
'LogSystemEvents()
'LogSecurityEvents()
LogApplicationEvents()
End Sub
Sub LogApplicationEvents()
Dim dtmDate = CreateObject("WbemScripting.SWbemDateTime")
Dim DateToCheck = CreateObject("WbemScripting.SWbemDateTime")
conn.ConnectionString = "Server=LocalHost; Integrated Security=true;
Database = SysMgt;"
'Get Oldest date time from table for server name
strSelect = "select * from tblApp Where ComputerName = '" &
strComputer & "' Order by TimeWritten DESC"
Try
objDataAdapter.SelectCommand = New SqlCommand(strSelect, conn)
objDataAdapter.Fill(objDataSet)
objDataTable = objDataSet.Tables(0)
If objDataTable.Rows.Count = 0 Then
DateToCheck = DateAdd(DateInterval.Day, -1, Now)
Else
DateToCheck = objDataTable.Rows(0).Item("TimeWritten")
End If
'dtmDate.SetVarDate(DateToCheck, True) '
strSearch = "Select * from Win32_NTLogEvent " _
& "Where LogFile = 'Application' and TimeWritten >= '" &
DateToCheck & "'"
moSearch = New Management.ManagementObjectSearcher(strSearch)
moReturn = moSearch.Get
Dim NewRow As DataRow
'The next line is causing me trouble.
NewRow = objDataSet.Tables("tblApp").NewRow()
For Each mo In moReturn
NewRow.Item("LogID") = "APP"
NewRow.Item("Category") = mo("CategoryString")
NewRow.Item("ComputerName") = mo("ComputerName")
NewRow.Item("EventCode") = mo("EventCode")
NewRow.Item("Message") = mo("Message")
NewRow.Item("RecordNumber") = CInt(mo("RecordNumber"))
NewRow.Item("SourceName") = mo("SourceName")
dtmDate = mo("TimeWritten")
strReturn = WMIDateStringToDate(dtmDate)
NewRow.Item("TimeWritten") = strReturn
NewRow.Item("EventType") = CInt(mo("EventType"))
NewRow.Item("User") = mo("User")
objDataSet.Tables("tblApp").Rows.Add(NewRow)
Next
objDataAdapter.Dispose()
objDataSet.Dispose()
objDataTable.Dispose()
conn.Close()
moReturn.Dispose()
moSearch.Dispose()
mo.Dispose()
Catch e As Exception
Console.WriteLine(e.ToString(), MsgBoxStyle.Exclamation, "Error
opening database")
End Try
objDataAdapter.Dispose()
objDataSet.Dispose()
objDataTable.Dispose()
conn.Close()
moReturn.Dispose()
moSearch.Dispose()
mo.Dispose()
End Sub
Function WMIDateStringToDate(ByVal dtmDate)
WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
Mid(dtmDate, 7, 2) & "/" & Mid(dtmDate, 1, 4) _
& " " & Mid(dtmDate, 9, 2) & ":" & _
Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate, 13, 2))
End Function
End Class
database & the event logs but hit a problem when trying to create a NewRow.
Follows is the code - scroll down I've indicated the line in question.
Thanks in advance for assistance.
BigSam
Imports System
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form1
Protected objDataAdapter As New SqlDataAdapter
Protected conn As New SqlConnection
Protected objCmd As New SqlCommand
Protected objDataSet As New DataSet
Protected objDataTable As New DataTable
Protected moReturn As Management.ManagementObjectCollection
Protected moSearch As New Management.ManagementObjectSearcher
Protected mo As New Management.ManagementObject
Protected strComputer As String = "Samsbox"
Protected strSelect As String
Protected strReturn As String
Protected strSearch As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
'LogSystemEvents()
'LogSecurityEvents()
LogApplicationEvents()
End Sub
Sub LogApplicationEvents()
Dim dtmDate = CreateObject("WbemScripting.SWbemDateTime")
Dim DateToCheck = CreateObject("WbemScripting.SWbemDateTime")
conn.ConnectionString = "Server=LocalHost; Integrated Security=true;
Database = SysMgt;"
'Get Oldest date time from table for server name
strSelect = "select * from tblApp Where ComputerName = '" &
strComputer & "' Order by TimeWritten DESC"
Try
objDataAdapter.SelectCommand = New SqlCommand(strSelect, conn)
objDataAdapter.Fill(objDataSet)
objDataTable = objDataSet.Tables(0)
If objDataTable.Rows.Count = 0 Then
DateToCheck = DateAdd(DateInterval.Day, -1, Now)
Else
DateToCheck = objDataTable.Rows(0).Item("TimeWritten")
End If
'dtmDate.SetVarDate(DateToCheck, True) '
strSearch = "Select * from Win32_NTLogEvent " _
& "Where LogFile = 'Application' and TimeWritten >= '" &
DateToCheck & "'"
moSearch = New Management.ManagementObjectSearcher(strSearch)
moReturn = moSearch.Get
Dim NewRow As DataRow
'The next line is causing me trouble.
NewRow = objDataSet.Tables("tblApp").NewRow()
For Each mo In moReturn
NewRow.Item("LogID") = "APP"
NewRow.Item("Category") = mo("CategoryString")
NewRow.Item("ComputerName") = mo("ComputerName")
NewRow.Item("EventCode") = mo("EventCode")
NewRow.Item("Message") = mo("Message")
NewRow.Item("RecordNumber") = CInt(mo("RecordNumber"))
NewRow.Item("SourceName") = mo("SourceName")
dtmDate = mo("TimeWritten")
strReturn = WMIDateStringToDate(dtmDate)
NewRow.Item("TimeWritten") = strReturn
NewRow.Item("EventType") = CInt(mo("EventType"))
NewRow.Item("User") = mo("User")
objDataSet.Tables("tblApp").Rows.Add(NewRow)
Next
objDataAdapter.Dispose()
objDataSet.Dispose()
objDataTable.Dispose()
conn.Close()
moReturn.Dispose()
moSearch.Dispose()
mo.Dispose()
Catch e As Exception
Console.WriteLine(e.ToString(), MsgBoxStyle.Exclamation, "Error
opening database")
End Try
objDataAdapter.Dispose()
objDataSet.Dispose()
objDataTable.Dispose()
conn.Close()
moReturn.Dispose()
moSearch.Dispose()
mo.Dispose()
End Sub
Function WMIDateStringToDate(ByVal dtmDate)
WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
Mid(dtmDate, 7, 2) & "/" & Mid(dtmDate, 1, 4) _
& " " & Mid(dtmDate, 9, 2) & ":" & _
Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate, 13, 2))
End Function
End Class