How to use DISTINCT Keyword in mySQL using VB.Net

0
30

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:

  1. CREATE TABLE IF NOT EXISTS student (
  2. `Fullname` VARCHAR(255) NOT NULL,
  3. `Address` VARCHAR(255) NOT NULL,
  4. `Contact_No` int(255) NOT NULL,
  5. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

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 `student`(`Fullname`, `Address`, `Contact_No`)
  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
  2.  
  3. Public Class Form1
  4.  
  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
  10.  
  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: ianlavapiez@gmail.com

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 32 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here