i am using sql server 2005 and I think Linq generating queries for a different database version. I got the query string that linq was producing and pasted the linq directly in SQL Server Management studio and it failed with many errors. But the query works well if Linq is used. Any ideas what could be going on. If linq needs to know what database version to use how do I do that?
LINQ will create a parameterized query. You won't be able to directly copy the SQL into SSMS without adding definitions (and assigning values to) the parameters.
Related
The latest technology to access SQL databases from C# is Linq. Before we used ADO.NET with T-SQL queries. My question is: can we ALWAYS build an equivalent Linq query for any complex T-SQL statement (complex large query with many JOINS and statements)?
Do we still need to use old ADO.NET access for large and complex query building?
No, you cannot and should not use LINQ for everything SQL can do. For ex. PIVOT, UNPIVOT GROUPING, ROLLUP, CUBE. Also, Kindly let complicated query writing be handled by tool meant for that (complex sql query in SQL server). That way, your code and database as well will remain maintainable.
I'm building an application that allows users to input SQL query strings. These query strings will contain 0 or more parameters and 1 or more returned columns. Is there a standard approach to parsing SQL queries to extract these elements? Ideally, this would be without running the query, or even being connected to an instance of SQL Server.
A query string might look like this:
SELECT
Posts.ID,
Posts.Description
FROM Posts
WHERE Posts.Date > #StartDate
And from this, I'd like to extract a collections of column names ("Posts.ID", "Posts.Description") and a collection of parameters ("StartDate").
This doesn't seem like a particularly strange thing to do. Indeed, Microsoft do this in their reporting products (I've seen it in BIDS).
Is there a library I can use? Otherwise, what's the recommended approach?
I don't know of any way to get this info without connecting to a SQL Server but for SQL Server 2012 onwards there are some new System stored procedures that might help;
sp_describe_undeclared_parameters
sp_describe_first_result_set
Prior to SQL Server 2012 you could execute a query using SET FMTONLY ON to get the resultset schema but you'd need to know the parameters.
Hope this helps,
Rhys
I've just started reading about LINQ and there is one thing that's been bugging me. LINQ turns a LINQ query into a corresponding SQL query to a database server. Since I doubt that LINQ can convert any code to an SQL statement I must assume that there are some limits here.
Since I want to have the query that is sent to the server as restrictive as possible, what can LINQ turn into SQL and what can't LINQ turn into SQL?
For example where employee.name == "Test" looks pretty trivial to convert, but what about where isEven(employee.number)? Or something more complex?
Here is the MSDN page detailing which methods LINQ to SQL supports.
You will not be able to use your own method calls inside of the expression tree, as LINQ to SQL does not understand how to convert the methods into SQL.
Some ORMs (e.g. NHibernate) allow you to define your own generators for generating SQL (HQL in NHibernates case) for your own methods, I do not believe L2S supports this.
I am calling SQL Server 10 from Entity Framework in C# and want to get a query hint into the request. The database has indexes which operated normally from SQL run in Management Studio, but when calling the command from C# using Entity Framework in Visual Studio, the query planner chooses a full scan when there is already an index.
I am creating dynamic predicates to request data in the following form:
data.attributeText = data.vegaDB.attributeText.AsExpandable().Where(parentPredicate.Compile()).ToList();
where parentPredicate is dynamically generated equivalent of:
(parentID = p1) AND (attributeName = 'name OR ... ')
From which the SQL Server query plan generates:
SELECT
[Extent1].[attributeID] AS [attributeID],
[Extent1].[parentID] AS [parentID],
[Extent1].[typeID] AS [typeID],
[Extent1].[attributeName] AS [attributeName],
[Extent1].[attributeData] AS [attributeData]
FROM [dbo].[attributeText] AS [Extent1]
So replacing the [Extent1] with the index [IX_parentID], which the direct sql call uses, by some extra command which does a query hint in the initial c# call would seem the solution. I have had a look around but no success yet. Any idea how to frame the question?
Do u think this is the right solution?
Run an SQL trace to find out what SQL query is actually being generated for this statement.
Does your predicate or an equivalent actually appear in the query as seen by SQL Server?
If it does appear, then you need to run that query through the Index Tuning wizard or similar.
If not, that's your problem - that would mean that Entity Framework is loading the entire table into memory and applying the predicate itself.
Updated I am pretty sure that this last is exactly what is happening: The AsExpandable() is failing to translate your predicate to SQL, so it is generating code to read the entire table, then applying the predicate to the returned data.
The solution is to stop using AsExpandable and use AsQueryable instead. The only reason to use AsExpandable is if AsQueryable doesn't offer the functionality you need, and I don't think that's the case here.
Try updating statistics for the related tables in your database, if statistics are outdated non-optimal query plans are likely to be used for all queries.
I think we may have trouble with our existing project. For some reasons we have to switch from SQL Server to Sybase SQL Anywhere 11. now we trying to find a way continue use our existing LINQ code.
We wish we can still use L2S? If cannot, we wish we can use L2E, then we have to change to ADO.
how to generate dbml file from Sybase Anywhere 11? after that can we use sqlmetal to generate .cs files?
Linq to SQL only supports MS SQL Server. You might consider switching to Entity Framework if this is an option and if there is a provider fro Sybase SQL Anyware.
As far as I know there are also no plans to extend the Linq to SQL support to other databases, especially as MS pushes EF as the future database technology.