Search Data from Multiple Table Columns in mySQL using VB.Net

0
92

Search Data from Multiple Table Columns in mySQL using VB.Net

Search Data from Multiple Table Columns in mySQL using VB.Net.

In this tutorial, you will learn how to search data from multiple table columns in mySQL using VB.Net.

First, let’s create our project in Microsoft Visual Studio 2013 and on our blank Form1 designer, let’s drag a button, text box and data grid view. Here’s an example:

Search Data from Multiple Table Columns in mySQL using VB.Net

Next, on your mySQL we will add a new database and table with multiple columns.

  1. CREATE TABLE IF NOT EXISTS sample (
  2. `Firstname` VARCHAR(50) NOT NULL,
  3. `Middlename` VARCHAR(50) NOT NULL,
  4. `Lastname` VARCHAR(50) NOT NULL,
  5. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Adding table with columns.

  1. INSERT INTO `sample`(`Firstname`, `Middlename`, `Lastname`)
  2. ('Ian','Hero','Santos'),
  3. ('Kane','Luke','Lavapiez'),
  4. ('Annika','Anne','Mald');

Adding values to our table.

Next, we are going to add this line of code to our search text box text changed event and form load event.

  1. Dim conn As MySqlConnection
  2. Dim cmd As MySqlCommand
  3. Dim da As MySqlDataAdapter
  4. Dim dt As DataTable
  5. Dim sql As String
  6.  
  7. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  8. Try
  9. conn = New MySqlConnection
  10. conn.ConnectionString = "server=localhost; userid=root; password=; database=sampledatabase;"
  11. conn.Open()
  12. sql = "SELECT * FROM sample;"
  13. cmd = New MySqlCommand(sql, conn)
  14. da = New MySqlDataAdapter
  15. dt = New DataTable
  16. da.SelectCommand = cmd
  17. da.Fill(dt)
  18. DataGridView1.DataSource = dt
  19. Catch ex As MySqlException
  20. MsgBox(ex.Message)
  21. Finally
  22. conn.Close()
  23. da.Dispose()
  24. End Try
  25. End Sub

Code for form load event to get all the values from the mySQL database.

  1. Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
  2. Try
  3. conn = New MySqlConnection
  4. conn.ConnectionString = "server=localhost; userid=root; password=; database=sampledatabase;"
  5. conn.Open()
  6. sql = "SELECT * FROM sample WHERE CONCAT_WS(Firstname, Middlename, Lastname) LIKE '%" & TextBox1.Text & "%'"
  7. cmd = New MySqlCommand(sql, conn)
  8. da = New MySqlDataAdapter
  9. dt = New DataTable
  10. da.SelectCommand = cmd
  11. da.Fill(dt)
  12. DataGridView1.DataSource = dt
  13. Catch ex As MySqlException
  14. MsgBox(ex.Message)
  15. Finally
  16. conn.Close()
  17. da.Dispose()
  18. End Try
  19. End Sub

Code for text box text changed event for searching the data based on the input text on the search box.

To test our code, let’s run the program and let’s try to search for the name “Ian”. Here’s my result:

Search Data from Multiple Table Columns in mySQL using VB.Net

As you can see it searched on our Firstname column. Now, let’s try “Luke”. It is the middle of Kane Luke Lavapiez. So, we are expecting to get the Kane’s full name if we searched for “Luke”. Here’s my result:

Search Data from Multiple Table Columns in mySQL using VB.Net

So, our expected result meets the searched result!

You have successfully learned on how to Search Data from Multiple Table Columns in mySQL using VB.Net.

For questions or any other concerns or thesis/capstone creation with documentation, you can contact me through the following:

E-Mail: [email protected]

Facebook: facebook.com/kirk.lavapiez

Contact No.: +639771069640

Ian Hero L. Lavapiez

BSIT Graduate

System Analyst and Developer

Related topic(s) that you may like:

 

 

 

Facebook Comments
(Visited 97 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here