Can anyone help me with importing text file to sql server using vb.net
here is my code
Dim ImportedFile As String
Dim ImportedLines() As String
Dim OneLine() As String
Dim strSQL As String
Dim i As Integer
If My.Computer.FileSystem.FileExists("D:\PPOS\Sales_report\inventory_255503221452_Cake102.CSV") Then
' Read the whole text file
ImportedFile = My.Computer.FileSystem.ReadAllText("D:\PPOS\Sales_report\inventory_255503221452_Cake102.CSV")
' Separate lines
ImportedLines = Strings.Split(ImportedFile, Environment.NewLine)
For i = 0 To ImportedLines.GetUpperBound(0)
' Separate fields
OneLine = Strings.Split(ImportedLines(i), ",")
If OneLine.GetUpperBound(0) = 8 Then
' Build SQL insert statement
strSQL = "INSERT INTO inventory(Inventory_id,item_id,Amount,AvgCost,CreateDate,CreateUser,Real_Qty,UnitID,BranchID) VALUES (" & OneLine(0) & ", " & OneLine(1) & ", " & OneLine(2) & ", " & OneLine(3) & ", '" & objgen.ChgDate(CDate(OneLine(4).ToString.Replace("'", "")), 0) & "', " & OneLine(5) & ", " & OneLine(6) & ", " & OneLine(7) & ", " & OneLine(8) & ")"
objacc.ExecNonquery(strSQL)
Else
MessageBox.Show("Wrong number of fields", "Notification", MessageBoxButtons.OK)
' Error: Wrong number of fields
End If
Next i
Else
MessageBox.Show("No record found.", "Notification", MessageBoxButtons.OK)
End If
End Sub
What happen is the For loop got excecuted 3 times more than the actual lines i have got in the text file
here is the data contains in the file
9,15,4440,60,'29/08/2553 11:49:10','Admin',72,21,'Cake101'
1 0,16,4100,100,'29/08/2553 11:49:47','Admin',41,21,'Cake101'
Please help me.
Every comment appreciated.
Thanks.