gpt4 book ai didi

mysql - CommandText 属性尚未正确初始化

转载 作者:行者123 更新时间:2023-11-29 14:15:27 28 4
gpt4 key购买 nike

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

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

最佳答案

我发现您的代码没有错误,但我怀疑您的脚本中有空行,因此允许它在您的命令文本上传递空字符串。请将文件的读取修改为此,

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

关于mysql - CommandText 属性尚未正确初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12773456/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com