I am trying to import data from a .csv file into a MySQL database, but are getting an error : 'Input string was not in a correct format.'
I have searched and tried many solutions with no success.
First I have created a data table to store the data from the .csv file. If create a breakpoint in my vb.net code and test the data table, it seems fine, however when I try to update the adapter I get the error. This is the code for my data table and the filling thereof :
When I test dt here everthing seems OK.
I then use the following code to load the data from dt into MySQL :
What am I missing. The code was working perfectly for SQLite? I am including the MYSQL create table command as well as my .csv data as .txt
I get the error at this line :
composition.txt
MySQLTablecreate.txt.
Can someone please help or direct me to a tutorial to do this?
Regards
I have searched and tried many solutions with no success.
First I have created a data table to store the data from the .csv file. If create a breakpoint in my vb.net code and test the data table, it seems fine, however when I try to update the adapter I get the error. This is the code for my data table and the filling thereof :
Code:
Dim dt As New System.Data.DataTable
With dt
.Columns.Add("Vessel", System.Type.GetType("System.String"))
.Columns.Add("Currvol", System.Type.GetType("System.Double"))
.Columns.Add("Wine", System.Type.GetType("System.String"))
.Columns.Add("Winename", System.Type.GetType("System.String"))
.Columns.Add("Winebatch", System.Type.GetType("System.String"))
.Columns.Add("Comments", System.Type.GetType("System.String"))
.Columns.Add("Grower", System.Type.GetType("System.String"))
.Columns.Add("Growername", System.Type.GetType("System.String"))
.Columns.Add("Region", System.Type.GetType("System.String"))
.Columns.Add("Block", System.Type.GetType("System.String"))
.Columns.Add("Section", System.Type.GetType("System.String"))
.Columns.Add("Variety", System.Type.GetType("System.String"))
.Columns.Add("Vintage", System.Type.GetType("System.Int32"))
.Columns.Add("Sectionvol", System.Type.GetType("System.Double"))
.Columns.Add("Cdocket", System.Type.GetType("System.String"))
.Columns.Add("Wdocket", System.Type.GetType("System.String"))
.Columns.Add("Equivton", System.Type.GetType("System.Double"))
.Columns.Add("Costperlit", System.Type.GetType("System.Double"))
.Columns.Add("Payclass", System.Type.GetType("System.String"))
.Columns.Add("EZYVerslagDatum", System.Type.GetType("System.DateTime"))
.Columns.Add("FileGebruik", System.Type.GetType("System.String"))
End With
Using myReader As New TextFieldParser(fileinvoer, Encoding.Default)
myReader.TextFieldType = FieldType.Delimited
myReader.SetDelimiters(",")
myReader.HasFieldsEnclosedInQuotes = True
myReader.ReadLine()
Do While Not myReader.EndOfData
Dim myData() As String = myReader.ReadFields
dt.Rows.Add(myData(0),
myData(1),
myData(2),
myData(3),
myData(4),
myData(5),
myData(6),
myData(7),
myData(8),
myData(9),
myData(10),
myData(11),
myData(12),
myData(13),
myData(14),
myData(15),
myData(16),
myData(17),
myData(18),
FDate.ToString("dd/MM/yyyy h:mm:ss tt"),
fileinvoer.ToString)
Loop
End Using
I then use the following code to load the data from dt into MySQL :
Code:
Dim strsql As String = "Insert into EZYComposition (vessel,currvol,wine,winename,winebatch,comments,grower,growername,region,block,section,variety,vintage,sectionvol,cdocket,wdocket,equivton,costPerLit,payclass,eZYVerslagDatum,fileGebruik) " &
"values ( @Vessel,@Currvol,@Wine,@Winename,@Winebatch,@comments,@Grower,@Growername,@Region,@Block,@Section,@Variety,@Vintage,@Sectionvol,@Cdocket,@Wdocket,@Equivton,@CostPerLit,@Payclass,@eZYVerslagDatum,@FileGebruik) "
Using connection As New MySqlConnection(MySqlConnectionstring)
connection.Open()
Dim cmd As New MySqlCommand(strsql, connection)
With cmd.Parameters
.Add("@Vessel", DbType.String, 50, "vessel")
.Add("@Currvol", DbType.Decimal, 50, "currvol")
.Add("@Wine", DbType.String, 50, "wine")
.Add("@Winename", DbType.String, 50, "Winename")
.Add("@Winebatch", DbType.String, 50, "winebatch")
.Add("@Comments", DbType.String, 50, "comments")
.Add("@Grower", DbType.String, 50, "grower")
.Add("@Growername", DbType.String, 50, "Growername")
.Add("@Region", DbType.String, 50, "region")
.Add("@Block", DbType.String, 50, "block")
.Add("@Section", DbType.String, 50, "section")
.Add("@Variety", DbType.String, 50, "variety")
.Add("@Vintage", DbType.Int32, 50, "vintage")
.Add("@Sectionvol", DbType.Decimal, 50, "sectionvol")
.Add("@Cdocket", DbType.Int32, 50, "cdocket")
.Add("@Wdocket", DbType.Int32, 50, "wdocket")
.Add("@Equivton", DbType.Decimal, 50, "equivton")
.Add("@CostperLit", DbType.Decimal, 50, "costperLit")
.Add("@Payclass", DbType.String, 50, "payclass")
.Add("@FileGebruik", DbType.String, 50, "fileGebruik")
.Add("@EZYVerslagDatum", DbType.DateTime, 50, "eZYVerslagDatum")
End With
Dim adapter As New MySqlDataAdapter
adapter.InsertCommand = cmd
Dim iRowsInserted As Int32 = adapter.Update(dt)
End Using
For i As Integer = 0 To 50
Threading.Thread.Sleep(50)
Next
What am I missing. The code was working perfectly for SQLite? I am including the MYSQL create table command as well as my .csv data as .txt
I get the error at this line :
Code:
Dim iRowsInserted As Int32 = adapter.Update(dt)
composition.txt
MySQLTablecreate.txt.
Can someone please help or direct me to a tutorial to do this?
Regards