0

我已经在这个论坛上搜索了不同的论坛,但仍然没有运气。我尝试了不同的代码,到目前为止,我的查询如下所示:

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;")
4

1 回答 1

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
于 2013-05-14T10:13:02.003 回答