MYSQL中的“SELECT”语法与.NET中的“dtTable.Select”不同

时间:2011-06-22 18:44:16

标签: c# vb.net datatable sql datatable.select

我在下面使用MySQL Query Browser尝试了“SELECT”MYSQL语法,正常工作。当我将此语法用于dtTable.Select()时,错误是“表达式上的语法错误”。请帮帮我,谢谢。

Dim dtTable As DataTable = MyDataset.Tables("machine")
Dim sql As String = String.Format("SELECT product_name, operator_name, totalizer_name 
                                   FROM product, operator, totalizer_type, machine 
                                   WHERE product.product_id = machine.product_id AND 
                                         operator.operator_id = machine.operator_id AND 
                                         totalizer_type.totalizer_id = machine.totalizer_id 
                                   ORDER BY machine.machine_id ASC;")
Dim rowSearching() As DataRow
rowSearching = dtTable.Select(sql)  ' <--- (error in here - "Syntax error on expression")

If rowSearching.Length > 0 Then
   For Each dr As DataRow In rowSearching
      MessageBox.Show(CStr(dr.Item(0)) & " " & CStr(dr.Item(1)) & " " & CStr(dr.Item(2)))
   Next
End If

2 个答案:

答案 0 :(得分:2)

DataTable.Select不起作用。它只是在它的列上接受一个过滤器,其语法类似于sql中的where子句语法。

实施例

dtTable.Select('operator_id = 1')

将返回dtTableoperator_id = 1

的所有行

了解更多信息:

Filter syntax

DataTable.Select Method

答案 1 :(得分:0)

您对“数据表”对象http://msdn.microsoft.com/en-us/library/det4aw50.aspx

的选择方式有误

你需要沿着这些方向使用某些东西

Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim SQL As String

conn.ConnectionString = myConnString
conn.Open()

myCommand.Connection = conn
myCommand.CommandText = SQL

myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData)

请参阅http://www.vbmysql.com/articles/vbnet-mysql-tutorials/the-vbnet-mysql-tutorial-part-4获取体面的教程。