2012-10-07 341 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'。看到我更新的答案。 –

相關問題