0

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` ;

有什么想法可以让我朝着正确的方向前进???

4

1 回答 1

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
于 2012-10-07T23:56:06.423 回答