C# 针对 MS Access 的 LINQ asp.net 页面。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/194528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:32:23  来源:igfitidea点击:

LINQ asp.net page against MS Access .

c#asp.netlinqms-access

提问by leora

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.

我有一个使用 ADO 查询 MS Access 数据库的 ASP.Net 页面,作为学习练习,我想合并 LINQ。我有一张简单的表格,叫做 Quotes。

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

这些字段是:QuoteID、QuoteDescription、QuoteAuthor、QuoteDate。我想运行简单的查询,比如“给我 1995 年之后的所有报价”。

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

我将如何将 LINQ 合并到这个 ASP.Net 站点 (C#)

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

基本上,我的问题是 LINQ 是否适用于 MS Access ?

采纳答案by Ryan Lundy

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:

LINQ to SQL 不支持 Access(也就是说,没有用于 LINQ 的 Access/Jet 提供程序),但您可以使用 LINQ 查询数据集。这意味着您使用您的数据库中您可能需要的任何可能的数据填充您的数据集,然后您在客户端进行过滤。有了类型化的 DataSet 并使用 TableAdapter 对其进行 Fill() 之后,您可以执行以下操作:

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 allthe 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.

你必须决定这是否值得。您必须用所有引号填充您的数据集,然后使用 LINQ 仅过滤 1995 年之后的那些引号。当然,对于少量数据,为什么不呢?但是对于非常大量的数据,您需要确保它不会太慢。

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.

但是,如果您使用的是 DataSet,则可以编写成为新 TableAdapter 方法的自定义查询。因此,您可以将查询的正确 SQL 放入 TableAdapter 的 FillByYear() 方法中,并使用它来填充键入的 DataTable。这样你只会取回你需要的数据。

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

如果您走这条路线,请记住 Access/Jet 使用位置参数,而不是命名参数。所以代替

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

you'd use something like this:

你会使用这样的东西:

SELECT * FROM Quotes WHERE Year(QuoteDate) > ?

回答by Jon Skeet

I don't thinkLINQ 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.

认为LINQ to SQL 不支持 Access。但是,如果您的表足够小以适合内存,则 LINQ to DataSet 将使您可以非常轻松地查询数据表等 - 特别是强类型数据集。