TableAdapter Parameterized Query (Access DB)
How can I add a parameterized Access query to a TableAdapter.
For SQL Server I could simply say
SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax
FROM dbo.Customers
WHERE ContactName LIKE @ContactName
But it doesn’t work in case of access db. complains about @ ?
[1140 byte] By [
JobLot] at [2007-12-16]
Hello,
Access uses a ? as the parameter placeholder (SQL Server uses named parameters prefixed with the @ symbol)
So the query would look like this:
SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax
FROM Customers
WHERE (ContactName LIKE ?)
Hope that helps,
what if i need more than one parameter, would they both have '?'
say for instance
SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax
FROM Customers
WHERE ContactName LIKE ? AND PostalCode = ?
How would they be differentiated?
When you create the TableAdapter query the method signature will actually use the column names that are in the WHERE clause and differentiate for you. For your example the TableAdapter query would look like:
CustomersTableAdapter.Fillby(ContactName, PostalCode)
Basically, unnamed parameters need to be passed in in the same order as they appear in the query.
Hope that helps!