Jump to content

Connecting to a MySQL server using Visual Studio 2010 and VB.Net


Recommended Posts

Posted

I have just started to create a new program to help with my helpdesk, i want all the data from my table to be displayed on the form. i have the following coding already but it displays nothing in my DataGridView:

 

Imports System.Data

Imports System.Data.Odbc

 

 

Public Class Form1

 

Private Sub conn()

 

'Variables for ServerName, UserName & PassWord

Dim SQLServerName As String = "ServerName"

Dim SQLUserName As String = "UserName"

Dim SQLPassWord As String = "PassWord"

 

Try

'Variable for the connection string

Dim connectionString As String = "Driver={MySQL ODBC 3.51 Driver};Server=" & SQLServerName & ";Database=ictsupport;User=" & SQLUserName & "; Password=" & SQLPassWord & ";Option=3;"

 

'Instialising the connection string

Dim conn As New OdbcConnection(connectionString)

 

'Opening the connection

conn.Open()

 

'Creating query to pull data from table

Dim DataAdapter As New OdbcDataAdapter("SELECT ID FROM TableName", conn)

 

'Putting data into DataGridView

Dim DatSet As DataSet = New DataSet

DataAdapter.Fill(DatSet)

DataGridView1.DataSource = DatSet

 

Catch SQLError As Exception

MessageBox.Show("Error connecting to MySQL server " & SQLError.Message)

End Try

 

End Sub

 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

conn()

 

End Sub

 

End Class

 

 

but everytime i run the program it gives me an empty datagrid, if anyone has any idea on how to get the data in my form that would be great.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...