针对 MS Access 的 LINQ asp.net 页面. [英] LINQ asp.net page against MS Access .

查看:24
本文介绍了针对 MS Access 的 LINQ asp.net 页面.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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

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

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

推荐答案

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

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;

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

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.

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

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.

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

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

你会使用这样的东西:

SELECT * FROM Quotes WHERE Year(QuoteDate) > ?

这篇关于针对 MS Access 的 LINQ asp.net 页面.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆