我试图从MySQL获取一些值 - 列为行,反之亦然 - 在DataGridView
上显示。我有这个代码,应该在MySQL-
SET @header = CONCAT('SELECT \'sl\', ',
(SELECT GROUP_CONCAT(CONCAT(' \'', sl, '\'')) FROM cars where sl=1),
' LIMIT 0, 0');
SET @a = -1;
SET @line1 = CONCAT(
'SELECT \'Plate\',',
(
SELECT GROUP_CONCAT(
CONCAT(' (SELECT Plate FROM cars LIMIT ',
@a:=@a+1,
', 1)')
)
FROM cars where sl=1
));
SET @a := -1;
SET @line2 = CONCAT(
'SELECT \'Brand\',',
(
SELECT GROUP_CONCAT(
CONCAT(' (SELECT Brand FROM cars LIMIT ',
@a:=@a+1,
', 1)')
)
FROM cars where sl=1
));
SET @query = CONCAT('(',
@header,
') UNION (',
@line1,
') UNION (',
@line2,
')'
);
PREPARE my_query FROM @query;
EXECUTE my_query;
现在,当我尝试通过ExecuteNonQuery
命令运行此操作时,将所有这些代码保存在字符串中,我收到MySQLException
错误 - Fatal error encountered during command execution.
我试图将代码拆分为单独的字符串,但会弹出相同的错误。还尝试增加CommandTimeout
,但没有任何效果。
有没有特殊的方法来运行这些语句?或者代码有问题吗?请注意,这在命令行客户端上没有任何错误的情况下有效运行。
PS:代码用于Q#3288014 - 感谢Anax
编辑:
我找到了同样的工作,但都在VB中完成。
Dim sa() As String = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim sa2() As String = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
connect()
Dim reader As MySqlDataReader
execstr = "describe cars"
Dim cmd As New MySqlCommand(execstr, connection)
reader = cmd.ExecuteReader()
Dim i As Integer = 0
While reader.Read
sa(i) = reader.GetString(0)
i = i + 1
End While
reader.Close()
connection.Close()
connect()
execstr = "select*from cars where sl=1;"
Dim cmd2 As New MySqlCommand(execstr, connection)
reader = cmd2.ExecuteReader()
While reader.Read
For i1 As Integer = 0 To sa.Length - 1
sa2(i1) = reader.GetString(i1)
Next
End While
reader.Close()
connection.Close()
reader.Close()
connection.Close()
Dim t As New DataTable
t.Columns.Add(sa(0))
t.Columns.Add(sa2(0))
For y As Integer = 1 To sa.Length - 1
t.Rows.Add(sa(y), sa2(y))
Next
DataGridView1.DataSource = t
有趣地发现,在MySQL中的字符串中可以完成所有操作,在VB中需要这么多代码。
答案 0 :(得分:0)
您无法在单个或一系列ExecuteNonQuery
命令中执行此操作。这个例子显示了你将放在mysql上的存储过程中的代码。您使用DataAdapter或DataReader调用存储过程。