美好的一天,
我开发了一款管理运动计时的应用程序。我在Visual Studio 2013和MySQL 5.6,我使用的语言和数据库方面经验丰富。我已经广泛尝试通过阅读遇到的类似错误来解决我的问题。
我在MySQL DB中有一个表要导出到.csv文件,但是当我以编程方式执行该语句时,我收到“命令执行期间遇到致命错误”错误消息。
我将SQL命令打印到MsgBox,在MySQL Workbench中复制并粘贴命令,命令执行正常并创建.csv文件。任何帮助解决这个问题将不胜感激。
这是我的代码:
Private Sub FullSetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FullSetToolStripMenuItem.Click
Dim ExportQuery, SqlQuery As String
Dim Comm As New MySqlCommand
With sfdExportResults
.InitialDirectory = "C:\Users\"
.Title = "Export CSV Files"
.CheckPathExists = True
.DefaultExt = "csv"
.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*"
.FilterIndex = 1
.FileName = Me.EventCode
.ShowDialog()
End With
FullSetPath = Replace(FullSetPath, "\", "/")
ExportQuery = "select 'racetime." & Me.EventCode & "' into @tableName; " _
& "select """ & FullSetPath & """ into @outputFile; " _
& "select group_concat(concat(""'"",column_name, ""'"")) " _
& "into @columnNames from information_schema.columns where table_name=@tableName;" _
& "SET @query = CONCAT(""select * from ((SELECT "",@columnNames,"") UNION (SELECT * FROM '"",@tableName,""')) " _
& "as a INTO OUTFILE '"", @outputFile, ""' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'""); " _
& "PREPARE stmt FROM @query; " _
& "EXECUTE stmt;"
MsgBox(ExportQuery)
MsgBox(FullSetPath)
Try
Comm.Connection = Conn
Comm.CommandText = ExportQuery
Comm.ExecuteScalar()
MsgBox("CSV successfully exported to: " & FullSetPath)
Catch MySqlCmdExeError As MySqlException
MsgBox("Error exporting CSV file: " & MySqlCmdExeError.Message)
End Try
End Sub
在MySQL Workbench中成功执行的软件生成的命令文本:
选择'racetime.DD0010'INTO @tableName; 选择“C:/Users/Peter/Desktop/DD0010.csv”INTO @outputFile; SELECT group_concat(concat(“'”,column_name,“'”))INTO @columnNames FROM information_schema.columns WHERE table_name = @ tableName; SET @query = CONCAT(“SELECT * FROM((SELECT”,@ columnNames,“)UNION(SELECT * FROM'”,@ tableName,“'))AS INTO OUTFILE'”,@ outputfile,“'FIELDS TERMINATED BY ','由'\ n''终止的线条;); 准备stmt FROM @query; 执行stmt;
我之前成功使用了另一个创建.csv文件的命令,但没有列标题,因此我在上面构造的新命令给出了错误。上一个工作命令:
ExportQuery= "SELECT * FROM " & Me.EventCode & " INTO OUTFILE """ & FullSetPath & """ FIELDS TERMINATED BY ',';" 'LINES TERMINATED BY '\n'
提前感谢您的帮助。