About Me

My photo

I'm an undergraduate of Rajarata University of Sri Lanka reading Applied sciences degree. I'm really interested in Java Programming language and developing some software using Java

Thursday, February 2, 2012

Connecting Visual Basic 2010 with MySql database Cont...

Today We are going to learn that, How to write code in VB 2010 for connect to MySql Database

1. Open up new Visual Studio Project and create a form

2. Right Click on the form you create, and choose view code option

3. In the window you have shown, Type the code below

Imports MySql.Data.MySqlClient

the above line should be appear in the very top of the window that is, The above line should be type above public class formname

After that type the below code after the class declaration, That is after the line
public class formname

Dim ServerString As String = "Server=localhost;User Id=root;Password=pass;Database=dtbname"
Dim SQLConnection As MySqlConnection = New MySqlConnection

The above code line I use my server as localhost and user id as root password for the root is pass and the database is dtbname. You can use your own database details instead of the above

The second line of above code should be typed like what I have entered but you can change only the variable name SQLConnection to your own variable name

After that type the code below

SQLConnection.ConnectionString = ServerString

            If SQLConnection.State = ConnectionState.Closed Then
                MsgBox("Connection is closed")
            End If
        Catch ex As Exception
        End Try

Thats it you have connected to MySql database from Visual Basic 2010

Tomorrow we will show how to retrieve data from MySql table....