VB.Net SQL Query不能在VB中工作,但在Access中工作

时间:2015-03-07 13:48:07

标签: sql vb.net

我正在尝试解决为什么此查询不返回结果。 相同的查询在Access中工作正常,但在VB.Net中没有。 任何帮助,将不胜感激。 尝试填充数据集时代码失败。 感谢。

Function Populate_Month()

    Dim dbConnection As New OleDbConnection
    Dim dbConnectionStr As String
    Dim dbProvider As String
    Dim dbSource As String

    dbProvider = "Microsoft.Jet.OLEDB.4.0;"
    dbSource = "C:\Users\Scraps\Documents\Gossamer.mdb"

    dbConnectionStr = "Provider=" & dbProvider & "Data Source=" & dbSource
    dbConnection = New OleDbConnection(dbConnectionStr)
    dbConnection.Open()

    Dim dbAdapter As OleDbDataAdapter
    Dim dbDataSet = New DataSet
    Dim dbQueryStr As String
    Dim CurrentRow As Integer

    dbQueryStr = "SELECT * FROM Forecast_TDL" & _
         "WHERE Forecast_TDL.EIAC & Forecast_TDL.LCN & Forecast_TDL.Servicing & Forecast_TDL.Interval & Forecast_TDL.Interval_Type" & _
         "NOT IN (SELECT EIAC & LCN & Servicing & Interval & Interval_Type FROM Grouped_Servicings);"

    dbAdapter = New OleDbDataAdapter(dbQueryStr, dbConnection)

    dbAdapter.Fill(dbDataSet, "forecast")
    CurrentRow = 0
    MsgBox(dbDataSet.Tables("forecast").Rows(CurrentRow)("Interval"))

    dbConnection.Close()
    Return Nothing

End Function

2 个答案:

答案 0 :(得分:0)

从我的角度来看,你需要在“Where”和“Not in”之类的子句之前使用空格。并在查询结束时删除分号。

答案 1 :(得分:0)

无法在MS Access中运行。您必须执行以下操作:

SELECT * FROM Forecast_TDL 
WHERE Forecast_TDL.EIAC NOT IN(SELECT EIAC FROM Grouped_Servicings) AND 
Forecast_TDL.LCN NOT IN(SELECT LCN FROM Grouped_Servicings) AND
Forecast_TDL.Servicing NOT IN(SELECT Servicing FROM Grouped_Servicings) And 
Forecast_TDL.Interval NOT IN(SELECT Interval FROM Grouped_Servicings) And 
Forecast_TDL.Interval_Type NOT IN (SELECT Interval_Type FROM Grouped_Servicings)

<击>

在更好地理解问题之后,您可以避免使用JOIN比较连接字符串并查找不存在行的位置。

SELECT * FROM Forecast_TDL
LEFT OUTER JOIN Grouped_Servicings
ON Forecast_TDL.EIAC = Grouped_Servicings.EIAC AND
 Forecast_TDL.LCN = Grouped_Servicings.LCN AND
 Forecast_TDL.Servicing = Grouped_Servicings.Servicing AND
 Forecast_TDL.Interval = Grouped_Servicings.Interval AND
 Forecast_TDL.Interval_Type = Grouped_Servicings.Interval_Type
WHERE Grouped_Servicings.EIAC IS NULL OR
      Grouped_Servicing.LCN IS NULL OR
      Grouped_Servicing.Servicing IS NULL OR
      Grouped_Servicing.Interval IS NULL OR
      Grouped_Servicing.Interval_Type IS NULL