Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys,

I need urgent help on how to connect to mySQL database of my office website from a desktop application.

The affected part of my code snippet used is as listed below:

Imports System.IO
Imports Microsoft
Imports MySql
Imports MySql.Data.MySqlClient


Public Sub InitialiseAPP()
        Static loginTrial As Integer
        
        Try

            loginTrial += 1
            strDB_Connection = strDB_Database & ";" & strDB_DataSource & ";" & strDB_UserAuthentication
            connMySQL.ConnectionString = strDB_Connection
            cInsertUpdate.ConnectionString = strDB_Connection

            If connMySQL.State = ConnectionState.Open Then connMySQL.Close()

            connMySQL.Open()

            If connMySQL.State = ConnectionState.Open Then
                connMySQL.Close()
                If (strDB_DataSource <> My.Settings.DB_DataSource) _
                    Or (strDB_Database <> My.Settings.DB_Database) _
                    Or (strDB_UserName <> My.Settings.DB_UserID) _
                    Or (strDB_UserPassword <> My.Settings.DB_Password) Then
                    My.Settings.DB_DataSource = strDB_DataSource
                    My.Settings.DB_Database = strDB_Database
                    My.Settings.DB_UserID = strDB_UserName
                    My.Settings.DB_Password = strDB_UserPassword
                    My.Settings.Save()
                    My.Settings.Upgrade()
                End If
            End If
        Catch ex As Exception
            MsgBox("Error #" & Err.Number & vbCrLf & ex.Message, MsgBoxStyle.Exclamation, "Database Connection Error")
            If loginTrial > 3 Then
                MsgBox("Please contact administrator for access" & vbCrLf _
                        & "Application terminated!", MsgBoxStyle.Critical, "Intrusion Alert!")
                End
            End If

            GetNewDataSourceConnection(strDB_DataSource, strDB_Database, strDB_UserName, strDB_UserPassword)
            strDB_UserAuthentication = strDB_UserName & ";" & strDB_UserPassword
            If strDB_DataSource <> "" Then
                InitialiseAPP()
            End If
        End Try

    End Sub

    Public Sub GetNewDataSourceConnection(ByRef strDataSource As String, ByRef strDBName As String, ByRef strDBUserName As String, ByRef strDBPassWord As String)
        'GetNewDataSourceConnection = ""
        Try
            With DB_Connect
                .ShowDialog()
                strDataSource = .pstrServerAddress
                strDBName = .pstrDBName
                strDBUserName = .pstrUserName
                strDBPassWord = .pstrServerPassword
            End With
        Catch ex As Exception

        End Try

    End Sub


The DB_Connect in the GetNewDataSourceConnection() subroutine is a form where all parameter relating to the database are entered.
If I try localhost in the "Data Source" it will connect without stress but if I try to connect through our site "IP Address" I get the error below:
"Error 91: Object reference not set to an instance of an object"
If I try using the site address i.e. https//mysubdomain.mysite.edu, I get the following error
"Error #5: Unable to connect to any of the specified MySQL hosts"
With the last error, I have the conviction that connecting to the "IP address" is the best but how to address the missing link is my challenge.
I need urgent help please!!!!!!!
Posted

Have you had a look at http://www.connectionstrings.com/mysql/[^]

Standard
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

MySQL Specifying TCP port
Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;<br />
Pwd=myPassword;


The port 3306 is the default MySql port.

The value is ignored if Unix socket is used.

MySQL Multiple servers

Use this to connect to a server in a replicated server configuration without concern on which server to use.
Server=serverAddress1, serverAddress2, serverAddress3;Database=myDataBase;<br />
Uid=myUsername;Pwd=myPassword;
 
Share this answer
 
It is actually very easy to accomplish this as long as you have all the relevant information to make a successful connection to a web hosted MySQL server.

Here is code that I use to accomplish this:

VB
Public Const WebDB As String = "database=<mydatababasename>;server=<fullserverpath "mysql.microsoft.com">;User Id=<root>;Pwd=<BillIsPissed>"


"mysql.microsoft.com" - remove the quotes. Quotes were added for readability.

I define it as a constant because the connection string will not likely change for a long time.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900