• Welcome to ForumKorner!
    Join today and become a part of the community.

VB.NET MySQL [DISPlAYING DATA]

Wiggles

Active Member
Reputation
0
Displaying MySQL Data Using VB.NET

Requirements
Visual Basic Express/Professional
MySQL Reference for .NET: http://dev.mysql.com/downloads/connector/net/

Getting Started
Add the "MySQL Data" Reference.
Add
Code:
Imports MySql.Data.MySqlClient
to the top of the page, to use the MySQL reference.

Create a form with 1x Button and 1x DataGridView

If you are using more than one form you may want to create the following as a Public String in a module.

Add this code to the button:​
Code:
Dim conn As MySqlConnection
  conn = New MySqlConnection
conn.ConnectionString = "server= your MySQL Server;Port=your Port (Default 3306); user id=your Username; password= your pass; database=your database"
  Dim da As Common.DbDataAdapter 'useful later, may get errors
    Dim ds As DataSet = New DataSet
    Dim command As New MySqlCommand
    Dim adapter As New MySqlDataAdapter
    Dim SQL1 As String = "SELECT * FROM YourTableNameHere"
    Dim data As New DataTable

The above is saying that conn is a MySQL connection with the connection string.
Now add the following​
Code:
  Try
    conn.Open() 'Opens connection
    command.Connection = conn 'Declares that the command will use the connection "conn"
    command.CommandText = SQL1' Declares the command text
    adapter.SelectCommand = command 'Selects the command the adapter uses
    adaper.Fill(data) 'gets data from Datatable
    DataGridView1.DataSource = data 'Tells DataGridView1 to get data from "data"

  Catch ex As MySqlException
    MsgBox(ex.Message) ' Displays MySQL error in messagebox
End Try 'Ends Try

It should look something like this

Thanks for reading, If you have any questions, ask and I'll do my best to answer.


I realize this is one of the most simple and unsecure ways of doing this, but hey, it works.
~Wiggles
 

TwEaK

Member
Reputation
0
Very nice post also very useful to the new VB.NET & MySQL coders