1

我在 Access 中创建了一个查询,看起来像这样......

SELECT project_master.ProjectID, project_master.OracleNum, project_master.BudgetYear, project_master.ProjectNum, project_master.Description, project_master.Description, project_master.Location, project_master.Region, project_master.ContactOwner, project_master.ContactDesigner, project_master.ProjectPriority, project_master.StartDate, project_master.InitialCloseDate, project_master.CurrentBonus, project_master.CurrentQty, project_master.TotalQty, project_master.TotalQty, project_master.SpendingYTD, project_master.SpendingLTD, project_master.SpendingAnnualBudget, project_master.SpendingForecast, project_master.SpendingMinimumForecast, project_master.SpendingRemainingCommitted, project_master.SpendingSaveOver, project_master.SpendingPushPull, project_master.IsCanceled, project_master.UserComments, project_master.tmp_reporting_selected, project_master.rep_header, project_master.rep_budget, project_master.rep_work_completed, project_master.rep_work_planned_completed, project_master.rep_variance_reason, project_master.rep_work_to_complete, project_master.rep_cutovers, project_master.rep_issues_resolution, project_master.rep_variance_reason1, project_master.rep_work_to_complete1, project_master.rep_cutovers1, project_master.rep_issues_resolution1 FROM project_master WHERE (((project_master.ProjectID)=[projectID]));

现在我对 Access 一点也不熟悉,我正在尝试制作一个存储过程,但我只是看不到任何这样做的选项。我创建的这个查询似乎采用了 projectID 参数,但是当我尝试在 VB.NET 中调用它并将参数传递给它时,我得到了表中的每条记录,而不是我想要的记录。

Public Shared Function GetProjectByID(ByVal projectID As Integer) As DataTable
        Dim paramaterList As New List(Of DataParameter)
        paramaterList.Add(New DataParameter("@projectID", projectID, ParameterDirection.Input, OleDb.OleDbType.Integer))
        Return DAL.GetDataTableUsingReader("get_project_by_id", paramaterList)
End Function

Public Shared Function Create(ByVal projectID As Integer) As Project
        If projectID < 1 Then
            Throw New ArgumentException("The project ID is invalid.")
        End If
        Dim dt As DataTable = ProjectSQL.GetProjectByID(projectID)
        If dt.Rows.Count < 1 Then
            Throw New DataException("No project record found by the provided ID.")
        End If
        Return Repackage(dt)(0)
End Function

Friend Shared Function Repackage(ByVal dt As DataTable) As List(Of Project)
    Dim projectList As New List(Of Project)
    For i As Integer = 0 To dt.Rows.Count - 1
        Dim p As New Project
        Dim row As DataRow = dt.Rows(i)
        p.ProjectID = Convert.ToInt32(row("ProjectID"))
        p.OracleNum = Convert.ToString(row("OracleNum"))
        p.BudgetYear = Convert.ToInt32(row("BudgetYear"))
        'etc...
        projectList.Add(p)
    Next
    Return projectList
End Function

基本上,我调用 Create() 方法并将一个整数传递给它,但我得到的是返回的所有记录而不是一条记录。

4

1 回答 1

2
WHERE (((project_master.ProjectID)=[projectID]))

当数据库引擎看到[projectID]时,它不知道您打算将其作为参数。它认为您指的是同名字段。所以它返回project_master.ProjectID等于它自己的每一行,它应该是所有ProjectID不为空的行。

为参数指定一个不同的名称,该名称与数据源中的任何字段都不匹配。

WHERE (((project_master.ProjectID)=[which_projectID]))
于 2013-02-07T14:21:19.720 回答