仅供参考:使用VS 2010和Access 2010创建Windows桌面应用程序
在尝试查询和访问数据库时,我在VB.Net中遇到了一些奇怪的行为。我有一个SQL脚本,当在Access中运行时,会生成一个包含记录的数据集。但是,当我在VB.Net(连接到同一访问数据库)中运行相同的脚本时,查询不会产生任何结果。这是我的.Net脚本:
Dim strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & item.Text
Dim conn As System.Data.OleDb.OleDbConnection
Dim da As OleDbDataAdapter
Dim s As String = " SELECT" & vbCrLf _
& " GrantData.[Grant Number]," & vbCrLf _
& " OutputData.[Output], " & vbCrLf _
& " OutputData.PlanNumber, " & vbCrLf _
& " OutputData.ActualNumber" & vbCrLf _
& " FROM " & vbCrLf _
& " Themes, " & vbCrLf _
& " GrantThemes, " & vbCrLf _
& " GrantData," & vbCrLf _
& " OutputData" & vbCrLf _
& " WHERE" & vbCrLf _
& " Themes.ThemeID = GrantThemes.ThemeId AND" & vbCrLf _
& " GrantData.[Grant Number] = GrantThemes.[Grant Number] AND" & vbCrLf _
& " GrantData.[Grant Number] =OutputData.[Grant Number] AND" & vbCrLf _
& " ( " & vbCrLf _
& " (GrantData.Status in ('Cleared', 'Completed', 'Closed')) AND" & vbCrLf _
& " (Themes.Theme Like '*infrastructure*' or Themes.Theme Like '*construction*')" & vbCrLf _
& " )" & vbCrLf _
& " ORDER BY" & vbCrLf _
& " GrantData.[Grant Number];"
conn = New System.Data.OleDb.OleDbConnection(strConnectionString)
conn.Open()
Try
da = New OleDbDataAdapter(s, strConnectionString)
da.Fill(ds, sqlStatement)
Catch ex As Exception
Finally
End Try
所以,就像我说的,如果我运行它,数据集是空的(我在ds上有一个断点,可以查看数据集)。但是,如果我在SQL字符串上放置一个断点并将其复制到Access数据库中,它运行就好了。有人有什么建议吗?
答案 0 :(得分:6)
Like
外卡存在问题。
虽然这在Access会话中有效...
Themes.Theme Like '*infrastructure*' or Themes.Theme Like '*construction*'
您需要将%
替换为*
OleDb ...
Themes.Theme Like '%infrastructure%' or Themes.Theme Like '%construction%'
或者,如果您更喜欢的查询会从OleDb中获得与Access相同的结果,请使用ALike
代替Like
。
Themes.Theme ALike '%infrastructure%' or Themes.Theme ALike '%construction%'