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.
- Visual Studio 2008 or 2010
- 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.
Then go to control panel and select administrative tools
From the administrative tools menu. Select Data Sources (ODBC)
On the User DSN tab, just click on add
Scroll down until you find the mysql odbc driver. Highlight it then click on Finish.
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.
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.
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.
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
That’s how you connect vb.net with mysql using odbc. Hope you learned something. Thanks for reading.