How to connect MYSQL 5.0 database to vb.net

To be able to create this program you must have basic knowledge concerning mySQL 5.0 as well as vb.net.

Prerequisite:
1. mySQL Server 5.0
2. mySQL Dot Net Connector 5.1
3. VB.net Express edition/ VB.net Family

Steps:

1. Install first mySQL Server 5.0 and and mySQL Dot Net Connector.
2. Import the reference of mySQL DotNet Conenctor



How to add a reference to MySql.Data.dll" means you need to add a library
• Create a new project in Visual Studio
• In the Solution Explorer, under the project name, locate References and right-click on it. Select "Add Reference".
• In the "Add Reference" dialog, switch to the "Browse" tab and browse to the folder containing the downloaded connector. Navigate to the "bin" folder, and select the "MySql.Data.dll" file. Click OK

Code for reading data from mysql data base
=====================================================================================
Imports MySql.Data.MySqlClient
Imports MySql.Data.MySqlClient.MySqlException
Imports MySql.Data.MySqlClient.MySqlCommand
Public Class Form1
Public con As New MySqlConnection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=test1;User ID=root;Password='';")
Dim sql As MySqlCommand = New MySqlCommand("SELECT * FROM tablename", con)
Dim ds As DataSet = New DataSet()
Dim DataAdapter1 As MySqlDataAdapter = New MySqlDataAdapter()
con.Open()
DataAdapter1.SelectCommand = sql
DataAdapter1.Fill(ds)
End Sub
=====================================================================================

No comments: