Before you use this code, here are my assumptions:
Things to do:
Table Name: ContactInformation
Fields: Id (int, autoIncrement), Name (varchar(64)), ContactNumber (varchar(20))
Sample Contents:
Visual Basic .Net Code
Public Class Form1
Private cn As OdbcConnection
Private adp As OdbcDataAdapter
Private dt As DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn = New OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=Contacts; User=root;Password=;Option=3;")
Call cn.Open()
adp = New OdbcDataAdapter("select * from ContactInformation", cn)
dt = New DataTable()
Call adp.Fill(dt)
DataGridView1.DataSource = dt
End Sub
End Class
- You have background on database management system
- You know how to create a database in mysql
- You know how to add tables to the database
- You are familiar with Visual Basic .Net
- MySQL Connector
- Microsoft Visual Basic Express 2008 (Since this is what I'm using right now. You can use Visual Basic Express 2005 or the full version included in the Microsoft Visual Studio Package)
- MySQL Editor (I'm using WAMP's phpMyAdmin)
Things to do:
- Create a database in MySQL with at least 1 table. I'm using the following in this sample:
Table Name: ContactInformation
Fields: Id (int, autoIncrement), Name (varchar(64)), ContactNumber (varchar(20))
Sample Contents:
Id | Name | ContactNumber | |||
---|---|---|---|---|---|
1 | John Doe | (yyy) yyy-yyyy | |||
2 | Jane Doe | (xxx) xxx-xxxx |
Visual Basic .Net Code
- Create a new Visual Basic Windows Forms Application.
- Add a DataGridView to the form
- On the code view, overwrite the code with the following:
Public Class Form1
Private cn As OdbcConnection
Private adp As OdbcDataAdapter
Private dt As DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn = New OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=Contacts; User=root;Password=;Option=3;")
Call cn.Open()
adp = New OdbcDataAdapter("select * from ContactInformation", cn)
dt = New DataTable()
Call adp.Fill(dt)
DataGridView1.DataSource = dt
End Sub
End Class
Comments