How to Create and Drop MySQL Database Using Visual Basic.Net

In this tutorial, it covers the basics of creating and dropping of MySQL Database with visual Basic. MySQL is a leading open source database management system, multi user and a multithreaded database management system. MySQL database is available on most important OS platforms. It is one part of the very popular LAMP platform. Linux, Apache, MySQL, MySQL, PHP. But in my case, I am using XAMPP. To begin in this course, open visual basic and create a new project and name it as “ManageDb”. After this, we need to add objects to our form such as: groupbox, textbox, Button, Datagridview and a label. Then set up your object properties and arrange all objects like as shown below. This time, we’re going to add functionality to our application. To do this, double click our form and add the following code: This code will simply show all the database on the datagridview.
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         'sets the query for shpwing all the tables and store to sql variable
  3.         Dim sql As String = "SHOW DATABASES"
  4.         Try
  5.             'open connection
  6.             con.Open()
  7.             'bind the connection and query
  8.             With cmd
  9.                 .Connection = con
  10.                 .CommandText = sql
  11.             End With
  12.             'Gets or sets a Transact-SQL statement or stored procedure used to select records in the data source.
  13.             da.SelectCommand = cmd
  14.             da.Fill(publictable)
  15.             'get the datasource from publictable and passed to datagridview
  16.             DataGridView1.DataSource = publictable
  17.  
  18.             'dispose the dataadapter
  19.             da.Dispose()
  20.         Catch ex As Exception
  21.  
  22.             MsgBox(ex.Message)
  23.  
  24.         End Try
  25.         con.Close()
  26.         da.Dispose()
  27.     End Sub
Next,here’s the code for selecting the specific database name and display it to textbox provided.
  1. Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
  2.         txtdbname.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString
  3.     End Sub
Then, we’re going to add functionality to our “create” button. To do this, add the following code:
  1. Private Sub btncreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncreate.Click
  2.         Dim result As Integer
  3.         Dim sql As String = "CREATE DATABASE `" & txtdbname.Text & "`;"
  4.  
  5.         Try
  6.             con.Open()
  7.             With cmd
  8.                 .Connection = con
  9.                 .CommandText = sql
  10.                 'Executes a Transact-SQL statement against the connection and returns the number of rows affected.
  11.                 result = cmd.ExecuteNonQuery
  12.                 If result = 0 Then
  13.  
  14.                     MsgBox("No Database created!")
  15.                 Else
  16.  
  17.                     MsgBox("Database created Successfully!")
  18.  
  19.                 End If
  20.             End With
  21.         Catch ex As Exception
  22.  
  23.  
  24.             MsgBox(ex.Message)
  25.  
  26.         End Try
  27.         con.Close()
  28.  
  29.         Call Form1_Load(sender, e)
  30.  
  31.     End Sub
Next, add the following code for “Drop” button. And here’s the following code:
  1. Private Sub btndropdb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndropdb.Click
  2.         Dim result As Integer
  3.         Dim sql As String = "DROP DATABASE `" & txtdbname.Text & "`;"
  4.         Try
  5.             con.Open()
  6.             With cmd
  7.                 .Connection = con
  8.                 .CommandText = sql
  9.                 result = cmd.ExecuteNonQuery
  10.                 If result = 0 Then
  11.  
  12.                     MsgBox("No Database has been Dropped!")
  13.                 Else
  14.  
  15.                     MsgBox("Database Successfully dropped!")
  16.  
  17.                 End If
  18.             End With
  19.         Catch ex As Exception
  20.  
  21.  
  22.             MsgBox(ex.Message)
  23.  
  24.         End Try
  25.         con.Close()
  26.  
  27.         Call Form1_Load(sender, e)
  28.     End Sub
And here’s all the codes for this tutorial:
  1. Imports MySql.Data.MySqlClient
  2.  
  3. Public Class Form1
  4.     'Represents an SQL statement or stored procedure to execute against a data source.
  5.     Dim cmd As New MySqlCommand
  6.     Dim da As New MySqlDataAdapter
  7.     Public total As Integer
  8.     Dim publictable As New DataTable
  9.     'declare conn as connection and it will now a new connection because
  10.     'it is equal to Getconnection Function
  11.     Dim con As MySqlConnection = jokenconn()
  12.     Public Function jokenconn() As MySqlConnection
  13.         Return New MySqlConnection("server=localhost;user id=root;password=;database=")
  14.     End Function
  15.  
  16.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  17.         'sets the query for shpwing all the tables and store to sql variable
  18.         Dim sql As String = "SHOW DATABASES"
  19.         Try
  20.             'open connection
  21.             con.Open()
  22.             'bind the connection and query
  23.             With cmd
  24.                 .Connection = con
  25.                 .CommandText = sql
  26.             End With
  27.             'Gets or sets a Transact-SQL statement or stored procedure used to select records in the data source.
  28.             da.SelectCommand = cmd
  29.             da.Fill(publictable)
  30.             'get the datasource from publictable and passed to datagridview
  31.             DataGridView1.DataSource = publictable
  32.  
  33.             'dispose the dataadapter
  34.             da.Dispose()
  35.         Catch ex As Exception
  36.  
  37.             MsgBox(ex.Message)
  38.  
  39.         End Try
  40.         con.Close()
  41.         da.Dispose()
  42.     End Sub
  43.  
  44.  
  45.     Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
  46.         txtdbname.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString
  47.     End Sub
  48.  
  49.  
  50.  
  51.     Private Sub btndropdb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndropdb.Click
  52.         Dim result As Integer
  53.         Dim sql As String = "DROP DATABASE `" & txtdbname.Text & "`;"
  54.         Try
  55.             con.Open()
  56.             With cmd
  57.                 .Connection = con
  58.                 .CommandText = sql
  59.                 result = cmd.ExecuteNonQuery
  60.                 If result = 0 Then
  61.  
  62.                     MsgBox("No Database has been Dropped!")
  63.                 Else
  64.  
  65.                     MsgBox("Database Successfully dropped!")
  66.  
  67.                 End If
  68.             End With
  69.         Catch ex As Exception
  70.  
  71.  
  72.             MsgBox(ex.Message)
  73.  
  74.         End Try
  75.         con.Close()
  76.  
  77.         Call Form1_Load(sender, e)
  78.     End Sub
  79.  
  80.     Private Sub btncreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncreate.Click
  81.         Dim result As Integer
  82.         Dim sql As String = "CREATE DATABASE `" & txtdbname.Text & "`;"
  83.  
  84.         Try
  85.             con.Open()
  86.             With cmd
  87.                 .Connection = con
  88.                 .CommandText = sql
  89.                 'Executes a Transact-SQL statement against the connection and returns the number of rows affected.
  90.                 result = cmd.ExecuteNonQuery
  91.                 If result = 0 Then
  92.  
  93.                     MsgBox("No Database created!")
  94.                 Else
  95.  
  96.                     MsgBox("Database created Successfully!")
  97.  
  98.                 End If
  99.             End With
  100.         Catch ex As Exception
  101.  
  102.  
  103.             MsgBox(ex.Message)
  104.  
  105.         End Try
  106.         con.Close()
  107.  
  108.         Call Form1_Load(sender, e)
  109.  
  110.     End Sub
  111. End Class
This time, you can now test your application by pressing “F5”.

Comments

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'sets the query for shpwing all the tables and store to sql variable Dim sql As String = "SHOW DATABASES" Try 'open connection con.Open() 'bind the connection and query With cmd .Connection = con .CommandText = sql End With 'Gets or sets a Transact-SQL statement or stored procedure used to select records in the data source. da.SelectCommand = cmd da.Fill(publictable) 'get the datasource from publictable and passed to datagridview DataGridView1.DataSource = publictable 'dispose the dataadapter da.Dispose() Catch ex As Exception MsgBox(ex.Message) End Try con.Close() da.Dispose() End Sub

Add new comment