如何从桌面应用程序在线连接到MySQL数据库 [英] How do I connect to MySQL Database online from my Desktop Application

查看:109
本文介绍了如何从桌面应用程序在线连接到MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要紧急帮助,了解如何从桌面应用程序连接到我办公室网站的mySQL数据库。



我使用的代码片段的受影响部分如下所示:



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





GetNewDataSourceConnection()子例程中的DB_Connect是一个表格,其中与数据库相关的所有参数都是输入。

如果我在数据源中尝试localhost它将连接没有压力但如果我尝试con通过我们的网站IP地址我得到以下错误:

错误91:对象引用未设置为对象的实例

如果我尝试使用站点地址即https // mysubdomain.mysite.edu,我收到以下错误

错误#5:无法连接到任何指定的MySQL主机

With最后一个错误,我坚信连接到IP地址是最好的,但如何解决缺失的链接是我的挑战。

我需要紧急帮助请!!!!!!!



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!!!!!!!

推荐答案

您是否看过 http://www.connectionstrings。 com / mysql / [ ^ ]



标准

Server = myServerAddress; Database = myDataBase; Uid = myUsername; Pwd = myPassword; < br $>


MySQL指定TCP端口

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;





端口3306是默认的MySql端口。



如果使用Unix套接字,则忽略该值。



MySQL多个服务器



使用此选项连接到复制服务器配置中的服务器,无需考虑使用哪个服务器。



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;


实际上是只要您拥有与Web托管的MySQL服务器成功连接的所有相关信息,就很容易实现这一点。



这是我用来完成的代码这个:



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:

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





mysql.microsoft.com - 删除引号。为了便于阅读,添加了引号。



我将其定义为常量,因为连接字符串很可能不会长时间改变。



"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.


这篇关于如何从桌面应用程序在线连接到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆