Dim filename As String = "C:\text.txt"
Dim lines As String() = File.ReadAllLines(filename)
Dim query As String = "INSERT INTO tbl_data VALUES (@Id, @Name, @Email, @Address)"
Using con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=ชื่อฐานข้อมูล;User ID=root;Password=;")
con.Open()
Using cmd As New MySql.Data.MySqlClient.MySqlCommand(query, con)
For Each line In lines
Dim id, name, email, address As String
Dim split As String() = line.Split("|"c)
id = split(0)
name = split(1)
email = split(2)
address = split(3)
cmd.Parameters.Clear()
Dim pId As New MySql.Data.MySqlClient.MySqlParameter("@Id", MySqlDbType.VarChar)
pId.Value = id
cmd.Parameters.Add(pId)
Dim pName As New MySql.Data.MySqlClient.MySqlParameter("@Name", MySqlDbType.VarChar)
pName.Value = name
cmd.Parameters.Add(pName)
Dim pEmail As New MySql.Data.MySqlClient.MySqlParameter("@Email", MySqlDbType.VarChar)
pEmail.Value = email
cmd.Parameters.Add(pEmail)
Dim pAddress As New MySql.Data.MySqlClient.MySqlParameter("@Address", MySqlDbType.VarChar)
pAddress.Value = address
cmd.Parameters.Add(pAddress)
cmd.ExecuteNonQuery()
Next
MsgBox("เพิ่มเรคอร์ดเรียบร้อยแล้ว")
con.Close()
End Using
End Using