Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ asp.net page against MS Access .

I have a ASP.Net page using ADO to query MS access database and as a learning exercise i would like to incorporate LINQ. I have one simple table called Quotes.

The fields are: QuoteID, QuoteDescription, QuoteAuthor, QuoteDate. I would like to run simple queries like, "Give me all quotes after 1995".

How would i incorporate LINQ into this ASP.Net site (C#)

Basically, my question is does LINQ work for MS Access ??

like image 812
leora Avatar asked Oct 11 '08 19:10

leora


People also ask

Does VB support LINQ?

Visual Basic features that support LINQAnonymous types, which enable you to create a new type based on a query result.

When should you use LINQ in your program?

Readable code: LINQ makes the code more readable so other developers can easily understand and maintain it. Standardized way of querying multiple data sources: The same LINQ syntax can be used to query multiple data sources. Compile time safety of queries: It provides type checking of objects at compile time.

Can we use LINQ in asp net?

You can use LINQ through the LinqDataSource control, through the ObjectDataSource control, or by creating LINQ queries. When you use LINQ in a Web application, you might have to change the policy files for code-access security.


2 Answers

LINQ to SQL doesn't support Access (that is, there's no Access/Jet provider for LINQ), but you can query a DataSet with LINQ. This means that you fill your DataSet with any possible data from your database that you might need in your results, and then you filter on the client side. After you have a typed DataSet, and you Fill() it with a TableAdapter, you do something like this:

var year = 1995;  // you can pass the year into a method so you can filter on any year
var results = from row in dsQuotes
              where row.QuoteDate > year
              select row;

You'll have to decide whether this is worth it. You'd have to fill your DataSet with all the quotes, then use LINQ to filter on just those quotes that are after 1995. For a small amount of data, sure, why not? But for a very large amount of data, you'll need to make sure it won't be too slow.

If you're using a DataSet, though, you can write custom queries that become new TableAdapter methods. So you can put the correct SQL for your query in a FillByYear() method in your TableAdapter and use that to fill your typed DataTable. That way you're only getting back the data you need.

If you go this route, remember that Access/Jet uses positional parameters, not named parameters. So instead of

SELECT * FROM Quotes WHERE Year(QuoteDate) > @Year

you'd use something like this:

SELECT * FROM Quotes WHERE Year(QuoteDate) > ?
like image 194
Ryan Lundy Avatar answered Oct 16 '22 09:10

Ryan Lundy


I don't think LINQ to SQL supports Access. However, if your table is sufficiently small to fit into memory, LINQ to DataSet will let you query datatables etc pretty easily - especially strongly typed datasets.

like image 4
Jon Skeet Avatar answered Oct 16 '22 08:10

Jon Skeet