问题
I've created a query in Access that looks like this...
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]));
Now I'm not familiar with Access whatsoever and I'm trying to make a stored procedure but I just don't see any option to do so. This query I've created seems to take the projectID parameter but when I try to call it in VB.NET and pass it the parameter, I get every record in the table instead of the one I want.
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
Basically, I call the Create() Method and pass it an integer but I get all records returned instead of just one record.
回答1:
WHERE (((project_master.ProjectID)=[projectID]))
When the db engine sees [projectID]
, it does not know you intend that to be a parameter. It thinks you're referring to the field of the same name. So it returns every row where project_master.ProjectID
is equal to itself, which should be all rows whose ProjectID
is not null.
Give the parameter a different name, which doesn't match any of the fields in your data source.
WHERE (((project_master.ProjectID)=[which_projectID]))
来源:https://stackoverflow.com/questions/14752713/stored-procedure-parameterized-query-in-access-and-pass-parameter