简体   繁体   English

使用VB.Net将文本文件的内容导入MySQL

[英]Importing contents of text file into MySQL using VB.Net

Good day everyone. 今天是个好日子。 I am trying to create a program that will transfer all of the contents of a text file into a database. 我正在尝试创建一个程序,该程序会将文本文件的所有内容传输到数据库中。 So far, my code works, but my code only inserts the first line of the text file into the database. 到目前为止,我的代码可以运行,但是我的代码仅将文本文件的第一行插入数据库。 What should I add to solve the problem? 我应该添加些什么来解决问题? I am noob at programming sorry. 我对编程感到很抱歉。

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim filename As String = "C:\Users\user\desktop\swipe.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, 3)
        Query = "INSERT INTO tbl_entrance_swipe VALUES ('" + Loc + "','" + _date + "','" + time + "','" + temp + "','" + id + "')"
        Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=cph;User ID=root;Password=;")
        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
    End Sub
End Class

You are using File.ReadAllText which reads the complete text of the file. 您正在使用File.ReadAllText读取文件的完整文本。 You want to read line by line, therefore you can use File.ReadLines (deferred executed) or File.ReadAllLines (reads all into a String() ). 您想逐行读取,因此可以使用File.ReadLines (延迟执行)或File.ReadAllLines (将所有内容读取到String() )。

You should also use sql-parameters to prevent sql-injection and incorrect implicit type conversions or localization issues. 您还应该使用sql-parameters防止sql-injection和不正确的隐式类型转换或本地化问题。 Finally, use the Using statement to ensure that all unmanaged resources are disposed (fe the connection gets closed even on error): 最后,使用Using语句来确保处置所有非托管资源(即使出现错误,连接也会关闭):

Dim filename As String = "C:\Users\user\desktop\swipe.txt"
Dim allLines As String() = File.ReadAllLines(filename)
Dim query As String = "INSERT INTO tbl_entrance_swipe VALUES (@Loc, @Date, @Time, @Temp, @Id)"

Using con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=cph;User ID=root;Password=;")
    con.Open()
    Using cmd As New MySql.Data.MySqlClient.MySqlCommand(query, con)
        For Each line In allLines
            Dim loc, dt, time, temp, id As String
            loc = line.Substring(0, 3)
            dt = line.Substring(3, 8)
            time = line.Substring(11, 4)
            temp = line.Substring(15, 3)
            id = line.Substring(18, 3)
            cmd.Parameters.Clear()
            Dim pLoc As New MySql.Data.MySqlClient.MySqlParameter("@Loc", MySqlDbType.VarChar)
            pLoc.Value = loc
            cmd.Parameters.Add(pLoc)
            Dim pDate As New MySql.Data.MySqlClient.MySqlParameter("@Date", MySqlDbType.VarChar)
            pDate.Value = dt
            cmd.Parameters.Add(pDate)
            Dim pTime As New MySql.Data.MySqlClient.MySqlParameter("@Time", MySqlDbType.VarChar)
            pTime.Value = time
            cmd.Parameters.Add(pTime)
            Dim pTemp As New MySql.Data.MySqlClient.MySqlParameter("@Temp", MySqlDbType.VarChar)
            pTemp.Value = temp
            cmd.Parameters.Add(pTemp)
            Dim pId As New MySql.Data.MySqlClient.MySqlParameter("@Id", MySqlDbType.VarChar)
            pId.Value = id
            cmd.Parameters.Add(pId)

            cmd.ExecuteNonQuery()
        Next
        MsgBox("All records were inserted successfully")
        con.Close()
    End Using
End Using

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM