How to import contents of text file into mysql table in vb.net? -
i've searched different forums on one, still no luck. have tried different codes, far query looks this:
dim query string query = "load data local infile 'c:\swipeimport\a1_20130513.txt' table tbl_entrance_swipe (@var1)" & _ "set loc=substring(@var1,1,3)," & _ "date=substring(@var1,4,8)," & _ "time=substring(@var1,12,3)," & _ "temp=substring(@var1,16,3)," & _ "id=substring(@var1,19,10);" con.open() dim sql mysqlcommand = new mysqlcommand(query, con) dim integer = sql.executenonquery() if (i > 0) msgbox("record inserted") else msgbox("record not inserted") end if con.close() i want import contents of text file table, table has fields:
loc, date, time, temp, id. the content of text file looks this:
k0720130514045501018006d9566 where breakdown follows:
k07 ----> loc 20130514 ----> date 0455 ----> time 010 ----> temp 18006d9566 ----> id could help, cannot insert in table. problem code? give fatal error. please point me in right direction. appreciated. thank you.
edit:
here connection string:
dim con mysqlconnection = new mysqlconnection("data source=dev;database=mydb;user id=root;password=mypass;")
try below, you....
dim filename string = "c:\swipeimport\a1_20130513.txt" dim query string dim data string = system.io.file.readalltext(filename) dim loc, _date, time, temp, id string loc = data.substring(0, 3) _date = data.substring(3, 8) time = data.substring(11, 4) temp = data.substring(15, 3) id = data.substring(18, 9) query = "insert tbl_entrance_swipe values ('" + loc + "','" + _date + "','" + time + "','" + temp + "','" + id + "')" dim con mysqlconnection = new mysqlconnection("data source=dev;database=mydb;user id=root;password=mypass;") try con.open() dim sql mysqlcommand = new mysqlcommand(query, con) sql.executenonquery() msgbox("record inserted") con.close() catch ex exception con.close() msgbox("record not inserted" + ex.message) end try
Comments
Post a Comment