|
-
Sep 7th, 2011, 05:56 PM
#1
Thread Starter
New Member
Read from CSV file, write directly to data source
I'm a beginner to VB2008 and trying to "convert" an Access DB to a stand alone application. I have under estimated the differences between VBA and VB. Although I have many questions, I'll focus on this topic first.
The Access DB collects and maintains SNMP traps from a radio station. I import CSV files from a remote Unix server via FTP, parse the text line-by-line, and add to a table as a new record. So far, I can parse the text and add to a dataset but I want to write directly to the data source. I also need error handling that will trap errors for duplicate records. There is a Reconcile function that ensures that all lines in the CSV files are added to the table before the files are permanently deleted. Here's what I have so far:
Code:
Public Class clsRnm
Private msTrapTable As String
Private moTrap As clsRnmTrap
Private moAppendStat As clsTrapFileAppendStat
Private mlSystemID As Long
Public Sub New(ByVal lSystemID As Long)
mlSystemID = lSystemID
moTrap = New clsRnmTrap
moAppendStat = New clsTrapFileAppendStat
End Sub
Public Function AppendTrapFile(ByVal sFullPath As String) As clsTrapFileAppendStat
Dim oSR As StreamReader
Dim oTA As New RnmDataSetTableAdapters.tblRnmTrapsTableAdapter
Dim oStats As New clsTrapFileAppendStat
Dim iOK, iBad, iDup As Integer
On Error GoTo ErrProc
If My.Computer.FileSystem.FileExists(sFullPath) = False Then
oStats.Message = "Error: File Not Found"
GoTo ExitProc
End If
oStats.FileName = My.Computer.FileSystem.GetName(sFullPath)
oSR = New StreamReader(sFullPath)
Do Until oSR.EndOfStream
moTrap = SplitTrapLine(oSR.ReadLine)
If moTrap.EventType = "A" Then
If moTrap.Err = 0 Then
With moTrap
oTA.Insert( _
ArrivalID:=.ArrivalID, _
SystemID:=mlSystemID, _
TimeStamp:=.TimeStamp, _
EventDate:=.EventDate, _
ObjectName:=.ObjectName, _
LevelID:=.LevelID, _
Message:=.Message, _
ObjectClass:=.ObjectClass, _
RegionID:=.RegionID, _
SiteID:=.SiteID)
End With
iOK = iOK + 1
Else
'An error was encountered in the SplitTrapLine function
iBad = iBad + 1
End If
End If
DuplicateEntryResume:
Loop
oStats.ErrNum = 0
oStats.Message = "Complete: " & oStats.FileName & " " & iOK & "/" & iDup & "/" & iBad
oStats.EventsAdded = iOK
AppendStat.InvalidLines = iBad
oStats.DuplicateEvents = iDup
ExitProc:
AppendTrapFile = oStats
MsgBox(AppendTrapFile.Message)
Exit Function
ErrProc:
Select Case Err.Number
Case 5
' Error triggered on .ExecuteNonQuery when ArrivalID is a duplicate
iDup = iDup + 1
Resume DuplicateEntryResume
Case Else
oStats.ErrNum = Err.Number
oStats.Message = "Error: " & Err.Number & " - " & Err.Description
GoTo ExitProc
End Select
End Function
Private Function SplitTrapLine(ByVal sTrapLine As String) As clsRnmTrap
Dim sTrapElement() As String
On Error GoTo ErrProc
sTrapElement = Split(sTrapLine, ",")
With moTrap
.EventType = Trim(sTrapElement(0))
.TimeStamp = Trim(sTrapElement(1))
.EventDate = Trim(sTrapElement(2))
.ObjectName = Trim(sTrapElement(3))
.LevelID = Trim(sTrapElement(4))
.Message = Trim(sTrapElement(5))
.ObjectClass = Trim(sTrapElement(6))
.RegionID = Trim(sTrapElement(7))
.SiteID = Trim(sTrapElement(8))
.AocID = Trim(sTrapElement(9))
.TrapID = Trim(sTrapElement(10))
.SequenceNum = Trim(sTrapElement(11))
.Host = Trim(sTrapElement(12))
.User = Trim(sTrapElement(13))
.ArrivalTime = Trim(sTrapElement(14))
.ArrivalID = Trim(sTrapElement(15))
.Err = 0
End With
SplitTrapLine = moTrap
Exit Function
ErrProc:
moTrap.Err = Err.Number
End Function
End Class
Other suggestions that may help with technique are welcome.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|