我的大多数编程经验都是基于c的,所以我对SQL(特别是mySQL平台)的工作方式没有最清楚的理解。我的vb.net应用程序在我创建的计算机上完全正常工作。但是,如果我将其下载到另一台计算机上,则mySQL连接无法打开。从我在其他StackOverflow帖子上看到的,这就是连接字符串的错误。我的连接字符串看起来像这样:
“server = xxx.xxx.xxx.xxx; uid = username; pwd = password; database = db; default command timeout = 300”
同样,对于基本计算机,这完全没有任何问题。当我遇到麻烦时,我从另一台计算机上运行程序。我需要更改服务器号吗?我试过这样做,但程序仍然无法正常工作。我还需要在字符串中添加一个字段吗?或者我是否需要在每台计算机上以某种方式配置mySQL设置?我想以允许任何人在下载后立即使用它的方式更改程序。上面列出的任何方法都可以使用,还是完全不同的方法?
谢谢。
这是我用MySQL的一个简单类。用您的值替换[括号]中的所有内容。如果它不工作,请查看防火墙,并使用MySQL WorkBench创建用户/密码/权限/架构。确保您可以使用MySQL工作台连接到您的数据库,然后您知道所有设置都正确。
Imports MySql.Data.MySqlClient
Public Class mysql
'Connection string for mysql
Public SQLSource As String = "Server=[x.x.x.x];userid=[yourusername];password=[yourpassword];database=[defaultdatabaseifany];"
'database connection classes
Private DBcon As New MySqlConnection
Private SQLcmd As MySqlCommand
Public DBDA As New MySqlDataAdapter
Public DBDT As New DataTable
' parameters
Public Params As New List(Of MySqlParameter)
' some stats
Public RecordCount As Integer
Public Exception As String
Function ExecScalar(SQLQuery As String) As Long
Dim theID As Long
DBcon.ConnectionString = SQLSource
Try
DBcon.Open()
SQLcmd = New MySqlCommand(SQLQuery, DBcon)
'loads params into the query
Params.ForEach(Sub(p) SQLcmd.Parameters.AddWithValue(p.ParameterName, p.Value))
'or like this is also good
'For Each p As MySqlParameter In Params
' SQLcmd.Parameters.AddWithValue(p.ParameterName, p.Value)
' Next
' clears params
Params.Clear()
'return the Id of the last insert or result of other query
theID = Convert.ToInt32(SQLcmd.ExecuteScalar())
DBcon.Close()
Catch ex As MySqlException
Exception = ex.Message
theID = -1
Finally
DBcon.Dispose()
End Try
Return theID
End Function
Sub ExecQuery(SQLQuery As String)
DBcon.ConnectionString = SQLSource
Try
DBcon.Open()
SQLcmd = New MySqlCommand(SQLQuery, DBcon)
'loads params into the query
Params.ForEach(Sub(p) SQLcmd.Parameters.AddWithValue(p.ParameterName, p.Value))
'or like this is also good
'For Each p As MySqlParameter In Params
' SQLcmd.Parameters.AddWithValue(p.ParameterName, p.Value)
' Next
' clears params
Params.Clear()
DBDA.SelectCommand = SQLcmd
DBDA.Update(DBDT)
DBDA.Fill(DBDT)
DBcon.Close()
Catch ex As MySqlException
Exception = ex.Message
Finally
DBcon.Dispose()
End Try
End Sub
' add parameters to the list
Public Sub AddParam(Name As String, Value As Object)
Dim NewParam As New MySqlParameter(Name, Value)
Params.Add(NewParam)
End Sub
End Class