I am a beginner at this. But let me explain what I need to do and show you my code
- I have a CSV file. inside the CSV I have a projectnumber, city,state,country
- I have a SQL table with the same column
- I want to use vb.net to check if projectnumber exists in sql table if exists then I want to run update statement. if it does not exists then I want to run insert statement.
I have the program working . but I am just wondering if this would be the correct way or my code is some hack way of doing it.
LEGEND: DTTable is data table with CSV inside
DT is data table with SQL result data
First I fill insert all lines in the CSV into a data table
Dim parser As New FileIO.TextFieldParser(sRemoteAccessFolder & "text.csv") parser.Delimiters = New String() {","} parser.ReadLine() Do Until parser.EndOfData = True DTTable.Rows.Add(parser.ReadFields()) Loop parser.Close()
then I use oledbdataadapter to run the select query and fill another data table with the result of the select statement
SQLString = "select * from tblProjects where ProjectID='" & DTTable.Rows.Item(i).Item("ProjectNumber") & "'" da = New OleDb.OleDbDataAdapter(SQLString, Conn) da.Fill(dt)
then I run if statement
If dt.Rows.Count = 0 then SQLString = "INSERT STATEMENT HERE" oCmd = New OleDb.OleDbCommand(SQLString, Conn) oCmd.ExecuteNonQuery()
Else SQLString = "UPDATE STATEMENT HERE" oCmd = New OleDb.OleDbCommand(SQLString, Conn) oCmd.ExecuteNonQuery() End if
ALL above code is run inside a for loop, to go through all the lines in the CSV
For i = 0 To DTTable.Rows.Count - 1
what do you think? please advise
thank you