简体   繁体   English

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

[英]VB.Net SQL Query not working in VB but does in Access

I'm trying to work out why this query does not return results. 我试图找出为什么这个查询不返回结果。 The same query works fine in Access but not in VB.Net. 相同的查询在Access中工作正常,但在VB.Net中没有。 Any help would be appreciated. 任何帮助,将不胜感激。 The code Fails when trying to fill the data set. 尝试填充数据集时代码失败。 Thanks. 谢谢。

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

From my point of view you need spaces before the clauses like "Where" and "Not in". 从我的观点来看,你需要在“Where”和“Not in”之类的子句之前使用空格。 And remove the semicolone at end of the query. 并在查询结束时删除分号。

There is no way that works in MS Access either. 在MS Access中也没有办法。 You would have to do something like: 你必须做类似的事情:

 
 
 
 
  
  
  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)
 
 
  

After understanding the question better, you can avoid comparing concatenated strings by using a JOIN and finding where a row doesn't exist. 在更好地理解问题之后,您可以避免使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM