VB.NET,使用MySQL Winforms应用程序。在我的应用程序中,由于基金和连接可靠性的限制,我添加了通过使用邮件附件来更新它的功能。一切都很好。好吧,在应用程序表单加载甚至我有它检查文件的存在。如果存在,则对数据库进行更改。因此调用下面的Sub并将该文件的内容读入arrayList ...当应用程序从VS外部启动时,我收到“CommandText属性未正确初始化”错误。
Private Sub sqlUpdate(ByVal sqlScriptName As String)
Dim D As Boolean = BackupDatabase("C:\xxxxxxxx\temp.sql")
Dim objReader As New StreamReader(sqlScriptName)
Dim sLine As String = ""
Dim arrText As New ArrayList()
Do
sLine = objReader.ReadLine()
If Not sLine Is Nothing Then
arrText.Add(sLine)
End If
Loop Until sLine Is Nothing
objReader.Close()
For Each sLine In arrText
Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
Dim connString As String = My.Settings.storageConnectionString
conn.ConnectionString = connString
myCommand.Connection = conn
myCommand.CommandText = String.Empty
Try
myCommand.CommandText = sLine
conn.Open()
myCommand.ExecuteNonQuery()
Catch myerror As MySqlException
'MsgBox("There was an error updating the database: " & myerror.Message + vbNewLine + "PLEASE CONTACT THE DEVELOPER IMMEDIATELY")
End Try
Next
Return
End Sub
不确定问题的来源,并且一直执行完毕... sList包含有效的MYSQL命令,如下所示:
ALTER TABLE `storage`.`property_info` ADD COLUMN `pfam_pName` CHAR(200) NULL AFTER `pfam_Phone` ;
任何想法可以让我朝着正确的方向发送这个???
答案 0 :(得分:1)
我发现您的代码没有错误,但我怀疑您的脚本中有空行,因此允许它在命令文本上传递empty string
。请修改文件的读取,
Do
sLine = objReader.ReadLine()
If (Not sLine Is Nothing) Then
If sLine.Trim().Length <> 0 Then ' this will not add empty line '
arrText.Add(sLine)
End If
End If
Loop Until sLine Is Nothing
或
For Each sLine In arrText
If sLine.Trim().Length = 0 Then Continue For ' skips for empty line '
Try
myCommand.CommandText = sLine
myCommand.ExecuteNonQuery()
Catch myerror As MySqlException
MsgBox("There was an error updating the database: " & _
myerror.Message & vbNewLine & _
"PLEASE CONTACT THE DEVELOPER IMMEDIATELY")
End Try
Next