VB NET - 选择*来自问题

时间:2016-04-03 09:00:13

标签: sql vb.net ms-access

我有一些奇怪的例外 我有两个表的Access数据库,一个名为MYSB_DB,另一个名为Employee

当我想过滤类似的东西时,我正在使用下面的代码:

Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String = String.Empty ' 
Dim pName As String = String.Empty
Dim fName As String = String.Empty
Dim ColpName As String = String.Empty
Dim ColfName As String = String.Empty
Dim ch As Integer = InStr(1, cmbEmployees.Text, " ", CompareMethod.Text)

pName = cmbEmployees.Text.Substring(0, ch)
fName = cmbEmployees.Text.Substring(ch, cmbEmployees.Text.Length - ch)
ColpName = "שדה1"
ColfName = "שדה2"
sql = "SELECT * FROM Employee WHERE [" & ColpName & "]=" & pName & ";"
da = New OleDb.OleDbDataAdapter(sql, Me.EmployeeTableAdapter.Connection.ConnectionString)
da.Fill(ds, Me.MYSB_DataBaseDataSet1.Employee.TableName)

我只在代码中的表名之间进行更改 当我在MYSB_DB表中使用此代码时,代码运行良好,但是当我使用Employee表的代码时,我有一个例外。

任何想法为什么会发生?

1 个答案:

答案 0 :(得分:0)

pName是一个字符串,因此必须引用:

sql = "SELECT * FROM Employee WHERE [" & ColpName & "]='" & pName & "';"