Here is some code that I put together for an app I am working on that loads 2 XML files and a large CVS file to a dataset at startup. It runs fairly fast, but I would like a second opinion on what I could do to either make it more consise or faster. I'm newer to .NET as well, so if you see anything that isn't very ."net ish" let me know!
#Region "Imports"
Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Threading
#End Region
Class clsLoadTables
#Region "Properties and Shared Variables"
Private Shared pathTableTwo As String = My.Settings.pathTableTwo
Private Shared pathMainTable As String = My.Settings.pathMainTable
Private Shared pathBeneLifeExp As String = My.Settings.pathBeneLifeExp
Private _ds As New DataSet
Public Property ds() As DataSet
Get
Return _ds
End Get
Set(ByVal value As DataSet)
_ds = value
End Set
End Property
#End Region
#Region "Constructors"
Sub New()
loadBeneLifeExpTable()
loadMainRMDTable()
loadCSVTableII()
End Sub
#End Region
#Region "ClassMethods"
Public Sub loadCSVTableII()
Dim dt As DataTable = ds.Tables.Add("TableII")
Dim line As String = String.Empty
Dim counter As Short = 0
Dim reader As New StreamReader(pathTableTwo)
Dim errorString As New StringBuilder
Try
errorString.Append("The tableII csv file did not load properly")
errorString.Append(Environment.NewLine & Environment.NewLine)
errorString.Append("Make syre the tabel_II.csv file is in the project folder")
Catch ex As Exception
Throw
End Try
Try
While Not reader.EndOfStream()
line = reader.ReadLine()
Dim lineSep As List(Of String) = line.Split(",").ToList
If Not counter = 0 Then
dt.Rows.Add(lineSep.ToArray)
counter += 1
Else
For Each value As String In lineSep
dt.Columns.Add(value)
Next
counter += 1
End If
End While
Dim primarykey(0) As DataColumn
primarykey(0) = dt.Columns("Ages")
dt.PrimaryKey = primarykey
Catch ex As FileNotFoundException
MessageBox.Show(errorString.ToString)
Throw
Catch ex As Exception
Throw
Finally
reader.Close()
End Try
End Sub
Public Sub loadMainRMDTable()
Dim tempDs As New DataSet
Dim dt As New DataTable
Dim settings As New XmlReaderSettings
Dim errorString As New StringBuilder
Try
errorString.Append("The RMD table did not load properly!")
errorString.Append(Environment.NewLine & Environment.NewLine)
errorString.Append("Make sure that the file 'MainRMDTable.xml' is in the project folder")
Catch ex As Exception
Throw
End Try
Try
Dim xmlFile As XmlReader
xmlFile = XmlReader.Create(pathMainTable, settings)
tempDs.ReadXml(xmlFile)
dt = tempDs.Tables("Age")
dt.TableName = "MainRMDTable"
xmlFile.Close()
Dim primarykey(0) As DataColumn
primarykey(0) = dt.Columns("age")
dt.PrimaryKey = primarykey
ds.Merge(tempDs)
Catch ex As FileNotFoundException
Throw
Catch ex As Exception
MessageBox.Show(errorString.ToString)
Throw
Finally
errorString.Clear()
tempDs.Clear()
End Try
End Sub
Public Sub loadBeneLifeExpTable()
Dim dt As New DataTable
Dim settings As New XmlReaderSettings
Dim errorString As New StringBuilder
Try
errorString.Append("The bene life expectancy table did not load properly ")
errorString.Append(Environment.NewLine & Environment.NewLine)
errorString.Append("Make sure that the file 'beneLifeExpectancyTable.xml' is in the project folder")
Catch ex As Exception
Throw
End Try
Try
Dim xmlFile As XmlReader
xmlFile = XmlReader.Create(pathBeneLifeExp, settings)
ds.ReadXml(xmlFile)
dt = ds.Tables("Age")
dt.TableName = "BeneLifeExpectancyTable"
xmlFile.Close()
Dim primarykey(0) As DataColumn
primarykey(0) = dt.Columns("BeneLifeExpectancyTable")
dt.PrimaryKey = primarykey
Catch ex As Exception
MessageBox.Show(errorString.ToString)
MessageBox.Show(ex.Message & ex.StackTrace())
Throw
Finally
errorString.Clear()
End Try
End Sub
#End Region
End Class