0

我在从 Access 中提取的 C# TableAdapter 中看到了一些奇怪的行为。我有一个查询在 Access 中返回结果,在 TableAdapter 查询生成器执行查询窗口中返回结果,但在代码中失败,并且在使用预览数据对话框时失败。

问题似乎出在 DateTime 参数中。奇怪的是,它仅在放入嵌套的 select 语句时才会失败。当应用于整个查询时,它工作正常。不幸的是,我在嵌套部分需要它,但我总是得到“标准表达式中的数据类型不匹配”。如果我删除其他参数并将数据参数保留在嵌套查询中,该查询也可以工作。

作品:

SELECT        Employee.FirstName, TimeCard.ID
FROM            (((Employee INNER JOIN
                     MasterFilmUnit ON Employee.ID = MasterFilmUnit.ID) INNER JOIN
                     MasterDepartment ON Employee.ID = MasterDepartment.ID) LEFT OUTER JOIN
                         (SELECT        ID, WorkDate, EmployeeID
                           FROM            DailyTimeCard
                           WHERE (WorkDate = ?)) TimeCard ON TimeCard.EmployeeID = Employee.ID)

作品:

SELECT        Employee.FirstName, TimeCard.ID
FROM            (((Employee INNER JOIN
                     MasterFilmUnit ON Employee.ID = MasterFilmUnit.ID) INNER JOIN
                     MasterDepartment ON Employee.ID = MasterDepartment.ID) LEFT OUTER JOIN
                         (SELECT        ID, WorkDate, EmployeeID
                           FROM            DailyTimeCard
                           ) TimeCard ON TimeCard.EmployeeID = Employee.ID)
WHERE Employee.ProjectID=? AND WorkDate = ?

不工作:

SELECT        Employee.FirstName, TimeCard.ID
FROM            (((Employee INNER JOIN
                     MasterFilmUnit ON Employee.ID = MasterFilmUnit.ID) INNER JOIN
                     MasterDepartment ON Employee.ID = MasterDepartment.ID) LEFT OUTER JOIN
                         (SELECT        ID, WorkDate, EmployeeID
                           FROM            DailyTimeCard
                          WHERE WorkDate = ? ) TimeCard ON TimeCard.EmployeeID = Employee.ID)
WHERE Employee.ProjectID=?

编辑

我已经能够通过嵌套查询的员工部分来获得所需的输出,因此没有 where 语句适用于整个查询。我现在已经启动并运行,但同样,我无法弄清楚为什么原来的不起作用:

作品:

SELECT        Employee.FirstName, TimeCard.ID
FROM            ((((SELECT Employee.*
                          FROM            Employee Employee_1
                          WHERE        (ProjectID = ?)) Employee Left OUTER JOIN
                     MasterFilmUnit ON Employee.ID = MasterFilmUnit.ID) LEFT OUTER JOIN
                     MasterDepartment ON Employee.ID = MasterDepartment.ID) LEFT OUTER JOIN
                         (SELECT        DailyTimeCard.*
                           FROM            DailyTimeCard
                           WHERE        (WorkDate = ?)) TimeCard ON TimeCard.EmployeeID = Employee.ID)

任何帮助,将不胜感激。我难住了。

4

1 回答 1

0

参数占位符按照?它们出现的顺序进行处理,所以这个查询期望参数 1 是WorkDate,参数 2 是ProjectID

如果这仍然不起作用,请尝试将?参数标记包装在CDate()和/或(可能)CInt()以强制参数为正确的数据类型。

于 2013-08-21T00:47:05.850 回答