2013-05-14 67 views
0

我在這個搜索了不同的論壇,但仍然沒有運氣。 我已經嘗試了不同的代碼,到目前爲止我的查詢看起來是這樣的:如何將文本文件的內容導入到vb.net的mysql表中?

Dim query As String 
query = "LOAD DATA LOCAL INFILE 'C:\swipeimport\A1_20130513.txt' INTO 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 As MySqlCommand = New MySqlCommand(query, con) 

    Dim i As Integer = sql.ExecuteNonQuery() 
    If (i > 0) Then 
     MsgBox("Record is Successfully Inserted") 
    Else 
     MsgBox("Record is not Inserted") 
    End If 
    con.Close() 

我想導入文本文件到表中的內容,我的表有這個字段:

Loc, date, time, temp, id. 

內容文本文件看起來像這樣:

K0720130514045501018006D9566 

在細分如下:

K07  ----> Loc 
20130514 ----> date 
0455  ----> time 
010  ----> temp 
18006D9566 ----> id 

你能幫忙,我不能在我的表中插入這個。我的代碼有什麼問題? 它只是給一個致命的錯誤。請指向正確的方向。很感謝任何形式的幫助。謝謝。

編輯:

這裏是我的連接字符串:

Dim con As MySqlConnection = New MySqlConnection("Data Source=dev;Database=mydb;User ID=root;Password=mypass;") 
+0

它也可以幫助人們看到您的連接字符串 – 5uperdan 2013-05-14 09:23:37

+0

如果我將文本行存儲爲列表,它會是性能明智嗎?然後做子字符串? – mmr 2013-05-16 02:52:53

回答

0

嘗試像下面,它會幫助你....

 Dim filename As String = "C:\swipeimport\A1_20130513.txt" 
     Dim Query As String 
     Dim data As String = System.IO.File.ReadAllText(filename) 
     Dim Loc, _date, time, temp, id As 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 INTO tbl_entrance_swipe VALUES ('" + Loc + "','" + _date + "','" + time + "','" + temp + "','" + id + "')" 
     Dim con As MySqlConnection = New MySqlConnection("Data Source=dev;Database=mydb;User ID=root;Password=mypass;") 
     Try 
      con.Open() 
      Dim sql As MySqlCommand = New MySqlCommand(Query, con) 
      sql.ExecuteNonQuery() 
      MsgBox("Record is Successfully Inserted") 
      con.Close() 
     Catch ex As Exception 
      con.Close() 
      MsgBox("Record is not Inserted" + ex.Message) 
     End Try 
+0

它不會在我的文本文件中插入所有行。它只插入第一行...我必須重申所有行嗎? – mmr 2013-05-15 00:48:22

+0

我應該怎麼做才能重申過去的觀點? – mmr 2013-05-16 02:39:43

+0

我已經將文本行存儲爲列表,是性能方面的明智嗎? – mmr 2013-05-16 02:53:17

相關問題