ads_header

How To Connect MySQL Database In Visual Studio


How To Connect MySQL Database In Visual Studio



Preview



Hello friends, this post is the same as before that is related to MySQL database connection, only this time I will do it using Microsoft Visual Studio 2010.  This tutorial is about the same as the previous tutorial, which is a connection with a SQL Server database. There has been a lot of discussion about this tutorial, but I like to review for beginners who are just starting out in the world of programming.

For this tutorial, first need to install a third party application that is MySQL ODBC Driver that function to create DSN (Data Source Name) which we will enter into our coding. To download the application, you can visit the site below

Download MySQL ODBC Driver

If you have downloaded and installed it, the next step is to create a DSN (Data Source Name) as follows:

Click the Start Button and search Data Sources (ODBC). Approximately looks like the picture below


Click the Add button then a dialog box will appear to select the type of Driver you want to use. Please select according to the version of the application you have downloaded earlier. For example I use MySQL ODBC 5.3 then click Finish.

The next step you have to fill the configuration form as follows:


NOTES :

Data Source Name : The name of the DSN we will enter into the coding.
Description : Optional.
TCP/IP_Server : You can use IP Address as above picture or "localhost".
User : Your Web Server Username Name (I'm using Appserv).
Password : Your Web Server Password.
Database : Your database name.





When all is done you can press the OK button. With this you have successfully created a DSN for the database that you will connect later.

Now we open and create a new project in Microsoft Visual Studio 2010. After that you simply add 1 Button only in the form to test the connection. The next step is to create a Module. Why ? so you can save some variables you need in creating a whole application like Data Adapter, Command and so on.

By right click project and select Add → Module give name and enter the following code:

Imports System.Data.Odbc.OdbcConnection

Module koneksi
    Public conn As Odbc.OdbcConnection

    Sub koneksinya()
        conn = New Odbc.OdbcConnection("Dsn=crud")
        If conn.State = ConnectionState.Closed Then
            conn.Open()
            MsgBox("Connection Successfully")
        End If
    End Sub
End Module


When finished, we go back to the form and double-click Button 1, then enter the following code

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
        Call koneksinya()
    End Sub
End Class

If the connection is successful then a message will appear that the connection is successful, otherwise the program will show an error message.

For more details, please watch the video below:





No comments:

Powered by Blogger.