Connecting mysql and vb.net using odbc driver

Last time, I showed you how to connect vb.net and mysql using the connector.net.  This time I’ll be showing you how you can do it the easier way. By using mysql odbc driver. Odbc is an acronym of Open Database Connectivity which is mainly used on Windows systems to be able to connect to many different databas. Which can also be used by applications especially those applications that are using the .net framework. And yes, you’ve guessed it right. The applications that we are making using visual studio makes use of the .net framework. If you don’t know what .net framework is, read this wikipedia article.

 

Requirements

  1. Wampserver
  2. Visual Studio 2008 or 2010
  3. Mysql ODBC driver

First thing that you’ll gonna need to do is to download and install the requirements which I listed above. The default configuration will do, but you can customize it if you want.

 

Using ODBC

Then go to control panel and select administrative tools

image

From the administrative tools menu. Select Data Sources (ODBC)

image

On the User DSN tab, just click on add

image

Scroll down until you find the mysql odbc driver. Highlight it then click on Finish.

image

It will now prompt you to input your database credentials together with the DSN or Data Source Name that you want. You can put whatever you like in the DSN.  You can also use your name if you want it to be simple.

Here are the defaults:

User: root

Password: none

TCP/IP Server: localhost

You can create your own database from phpmyadmin then select it here. Or you can download this file and import it on your server.

image

After inputting all the details, just click on ok then ok again.

The next step is to create

 

Create Vb.net program

Next step is to create the application that would connect to the mysql database using odbc. Just add some buttons and textboxes to the form and create a new class where we will call the methods that will manipulate the database.

Place the code below at the uppermost portion of the class that you have created.

imports system.data.odbc

That will make your class inherit all the functions and methods which are included in the odbc library for manipulating database.

 

Next thing, declare all the fields that you will be storing in your database as a public string. So that it can be accessed in any windows form that you’re going to create.

Example:

public name as string

public course as string

 

Then declare the connection string.

Dim con as  new odbcConnection(“DSN=wern”)

I set my DSN to be equal to ‘wern’ since that is what I’ve entered a while ago. Scroll up so that you can see.

 

Then the command.

Dim com as odbcCommand

What we have to do now is to create a constructor that will open the connection for us and set the connection for the command to be equal to the connection string that we set a while ago.

public sub new()

con.open()

com=new odbcCommand(“SELECT  * FROM obfus”, cn)

end sub

Now for the finishing part, lets try to create a subroutine that would insert a new record into the database.

public sub inserts()

com.commandText=”INSERT INTO obfus(Name, Course) VALUES(‘” + name + “’ , ‘” + course + “’ )”

end sub

Conclusion

That’s how you connect vb.net with mysql using odbc. Hope you learned something. Thanks for reading.

5 thoughts on “Connecting mysql and vb.net using odbc driver

  1. Pingback: How to list mysql data in vb.net using data grid view « Data Integrated Entity

  2. Pingback: vb.net database connection basics part 2 « Data Integrated Entity

  3. Hey there! I know this is kinda off topic nevertheless I’d figured I’d ask.

    Would you be interested in trading links or maybe guest authoring a blog article or vice-versa?
    My site covers a lot of the same subjects as yours and I feel we
    could greatly benefit from each other. If you’re interested feel free to shoot
    me an email. I look forward to hearing from you!
    Great blog by the way!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s