Created
January 1, 2019 21:14
-
-
Save TanmayChakrabarty/24a2f70e2a4a1abcb053fe9d477585e3 to your computer and use it in GitHub Desktop.
Using MySQL Database with Visual Basic .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports MySql.Data.MySqlClient | |
Public Class Form1 | |
Public dbconn As New MySqlConnection | |
Public sql As String | |
Public dbcomm As New MySqlCommand | |
Public dbread As MySqlDataReader | |
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load | |
dbconn = New MySqlConnection("Data Source=localhost;user id=root;password=demo;port=3310;database=vb_mysql") | |
Try | |
dbconn.Open() | |
Catch ex As Exception | |
MsgBox("Connection Error: " & ex.Message.ToString) | |
End Try | |
End Sub | |
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click | |
sql = "INSERT INTO _users(u_id,u_firstName,u_lastName) VALUES(NULL,'" & txt_firstName.Text & "','" & txt_lastName.Text & "')" | |
Try | |
dbcomm = New MySqlCommand(sql, dbconn) | |
dbread = dbcomm.ExecuteReader() | |
dbread.Close() | |
MsgBox("Data inserted.") | |
Catch ex As Exception | |
MsgBox("Failed to insert data: " & ex.Message.ToString()) | |
End Try | |
dbread.Close() | |
End Sub | |
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click | |
listBox_users.Items.Clear() | |
sql = "SELECT * FROM _users" | |
Try | |
dbcomm = New MySqlCommand(sql, dbconn) | |
dbread = dbcomm.ExecuteReader() | |
While dbread.Read | |
listBox_users.Items.Add(dbread("u_firstName").ToString() & " " & dbread("u_lastName").ToString()) | |
End While | |
Catch ex As Exception | |
MsgBox("Problem loading data: " & ex.Message.ToString) | |
End Try | |
dbread.Close() | |
End Sub | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read detail tutorial at
https://www.onlineclassnotes.com/2013/01/using-mysql-database-with-visual-basic.html