How to use DISTINCT Keyword in mySQL using VB.Net


How to use DISTINCT Keyword in mySQL using VB.Net

How to use DISTINCT Keyword in mySQL using VB.Net.

In this tutorial, you will learn how to use the DISTINCT keyword using VB.Net.

First, we are going to create a database named distincttable. Then, we will add a table.

On your XAMPP Control Panel, click your database and click the SQL category to insert this line of code to create a table with columns:

  2. <code>ID</code> int(10) NOT NULL AUTO_INCREMENT,
  3. <code>Fullname</code> VARCHAR(255) NOT NULL,
  4. <code>Address</code> VARCHAR(255) NOT NULL,
  5. <code>Contact_No</code> int(255) NOT NULL,
  6. PRIMARY KEY (<code>ID</code>)

Next, we are going to add values to our table. Click your student table and then click the SQL category to insert this line of code:

  1. INSERT INTO <code>student</code>(<code>Fullname</code>, <code>Address</code>, <code>Contact_No</code>)
  2. ('Ian','Hero','21312'),
  3. ('Ian','Hero','89879'),
  4. ('Jose','De la Cruz','68768'),
  5. ('Jose','De la Cruz','03244'),
  6. ('Pablo','Tingco','34234'),
  7. ('Pablo','Tingco','42343');

Next, create your VB project and drag a datagridview. Take note that you need the Mysql.Data.dll to access the mySQL methods. Also, set your datagridview AutoSizeColumnsMode to “Fill”.

Add the Mysql.Data.dll to your reference to access the mySQL methods and parameters.

At your Form1 designer, double click it and add this line of code:

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

Run your system and your expected output should be like this:

How to use DISTINCT Keyword in mySQL using VB.Net

Now, as expected we have duplicate values in the Fullname and Address columns. In order to just show up at least one value representing them, we are going to use the DISTINCT keyword of mySQL.

DISTINCT is used to return only one value from duplicate values. This is helpful especially when you want to just get the list of values without duplication.

Now, we are going to change our sql string and add the DISTINCT keyword.

  1. sql = "SELECT DISTINCT Fullname, Address FROM student;"

Load again your project and you will see the results that you can only see one value from different same values.

Congratulations! You have learned today how to use DISTINCT keyword 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]


Contact No.: +639771069640

Ian Hero L. Lavapiez

BSIT Graduate

System Analyst and Developer

Related topic(s) that you may like:



Facebook Comments


Please enter your comment!
Please enter your name here