LINQ asp.net page against MS Access .

后端 未结 2 1067
悲哀的现实
悲哀的现实 2020-12-11 03:16

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

相关标签:
2条回答
  • 2020-12-11 03:41

    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.

    0 讨论(0)
  • 2020-12-11 03:42

    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) > ?
    
    0 讨论(0)
提交回复
热议问题