Erm's I.T. Girl – Zelna Ellis

Don't fear when Zel is near…

.NET & MySQL Part 4

Setup MySQL Connection String for a Windows application using VB.NET

Software used in this post:

In Visual Studio

  • Create a Windows Form Application named TestConn.
    frmTest only has two buttons: btnTest and btnExit.
    Fig. 1
  • Add Reference
    From the menu select Project | Add Reference
    Scroll down, select MySQL.Data, and click OK.
    Fig. 2
  • The Program Code

    Imports MySql.Data.MySqlClient

    Public Class frmTest
      Dim MySQLConnectionString As String
      Dim MyADOConnection As MySqlConnection

     Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
      Try
        MySQLConnectionString =     “Server=hostname;” & _
        “Database=mydbschema;” & _
        “Uid=myusername;” & _
        “Pwd=mypassword;” & _
        “Connect Timeout=30;”

        MyADOConnection = New MySqlConnection()
        MyADOConnection.ConnectionString = MySQLConnectionString
        MyADOConnection.Open()
        MessageBox.Show(“Connection Opened Successfully!”, “Test Connection”)
      Catch ex As Exception
        MessageBox.Show(“Error Connecting to Database: ” & Err.Description, “Test Connection”))
      End Try
     End Sub

     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
      Try
        MyADOConnection.Close()
        MyADOConnection.Dispose()
      Catch ex As Exception
        ‘Do Nothing
      End Try
      Application.Exit()
     End Sub
    End Class

  • Run the program
    Click the Test button. The following message should display if the connection was successful:
    Fig. 3
    Click the Exit button

Conclusion:
Read here more about the connection strings.

In this example the connection string was hardcoded (embedded). This is not recommended!

    Why?

  1. A malicious Connection String injection attack can occur. The injection attacker can completely destroy the database or retrieve important secure information.
  2. If any of the Connection String parameters change, the entire applications needs to be recompiled and redeployed again to all the computers.

🙂 Happy Coding

In the next post .NET & MySQL Part 5 we will have a look how to enable SSL for MySQL Server.

Previous Posts:
.NET & MySQL Part 1 A list of software required as well as optional software that can be used.
.NET & MySQL Part 2 Install MySQL Server
.NET & MySQL Part 3 Install PHP on Windows XP IIS Server

Version 1.1

26 March 2009 - Posted by | .NET & MySQL | , , ,

No comments yet.

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

%d bloggers like this: