访问和传递参数中的存储过程/参数化查询

时间:2013-02-07 13:50:54

标签: .net ms-access stored-procedures parameter-passing

我在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()方法并传递一个整数,但我得到的所有记录都返回而不是只返回一条记录。

1 个答案:

答案 0 :(得分:2)

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

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

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

WHERE (((project_master.ProjectID)=[which_projectID]))