2012-10-07 345 views
0

VB.NET,带有MySQL的Winforms应用程序。在我的应用程序中,由于资金和连接可靠性的限制,我添加了通过使用邮件附件来更新它的功能。一切正常。那么在应用程序表单加载,即使我有它检查文件的存在。如果存在,则会对数据库进行更改。所以下面的Sub被调用,并且该文件的内容被读入到一个arrayList中...当应用程序从外部VS启动时,我得到“CommandText属性未正确初始化”错误。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

快速评论别人提起这件事之前..我才意识到我不应该将其发送到一个ArrayList,我可以在每行上执行NonQuery而不是将其发送到ArrayList。 – Skindeep2366

回答

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 
+0

我还没有测试过这个,但我确实记得在sLine中显示的一个空字符串,当我正在进行查看时发生了什么。猜猜我没有付钱给它,因为txtfile从中读取行没有空行..非常感谢您的帮助.. – Skindeep2366

+0

@ Skindeep2366或者添加如果sLine.Trim()。Length = 0然后Continue For'跳过空sLine'。看到我更新的答案。 –

相关问题