仅供参考:使用 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 数据库中,它运行得很好。有人有什么建议吗?