2
votes

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.

2
When you say "run it through" - you mean executing the actual query, or trying the generated SQL through a profiler? - Jon Skeet
Are you using Linq to Sql or Entity Framework? - Phil
@JonSkeet I'm executing the actual query ("though" not "through"). The SQL I pasted is what shows up in the profiler, and the debugger when I try to evaluate the result. - ilitirit

2 Answers

0
votes

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.

-3
votes

If you are using an ODBC provider, it will not work. Change to SqlClient.