问题
I've created this simple LINQ query:
var result = from invoice in invoiceTable
where invoice.Id == 1
select invoice.Document;
It generates this SQL:
SELECT [t0].[Document]
FROM [Invoice] AS [t0]
WHERE [t0].[Id] = @p0
Whenever I run it though I get this error:
(ODBC)
ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the variable '@p0'.
(OleDb)
Must declare the variable '@p0'.
I remember reading that OleDb and ADO.NET does not support named parameters on SQL Server 2000. Is this problem related to that issue, or am I doing something else wrong?
UPDATE 1:
This does seem to be a provider issue. I tried the same query against SQL Server 2008 using the SQL Client data provider and it worked fine. Unfortunately, this provider does not work with SQL Server 2000.
When I tried using the ODBC and OLEDB providers with SQL Server 2008 I got the same errors.
Does anyone know of a suitable workaround?
UPDATE 2:
It turns out the SQL Client provider does work with SQL Server 2000. Just not from within Visual Studio 2010. I changed the provider and the query works now.
回答1:
It turns out the SQL Client provider does work with SQL Server 2000. Just not from within Visual Studio 2010. I changed the provider and the query works now.
回答2:
If you are using an ODBC provider, it will not work. Change to SqlClient.
来源:https://stackoverflow.com/questions/10016456/linq-to-sql-server-2000-must-declare-variable-p0