ADO.NET 还是 Linq to SQL?
我正在建立一个论坛,它有 4 个表:用户、线程、评论、主题。
我建立了连接和页面。我开始使用 ADO.net 方式插入数据和选择数据。但后来我发现要进行更复杂的操作,我需要了解 SQL。所以我正在寻找另一种方法,我发现我可以打开Visual Studio 2010,将Linq添加到生成对象关系设计器的SQL文件中。我阅读了有关如何编写代码的信息,发现我只需要使用带有 DataContext
对象的 using 语句和简单的代码来更新、添加、删除表中的行。
我想知道,使用一种查询方式相对于另一种查询方式有什么优势?
I am building a forum, and it has got 4 tables: Users, Threads, Comments, Topics.
I established the connection and the pages.. I started using the ADO.net way to insert data and select data..but then I found that to make more complex manipulations i need to know SQL. So I was looking for another way, and I found that I can open Visual Studio 2010, add Linq to SQL file that produced object relational designer. I read about how to write code, and I saw that I simply need to use a using statement with DataContext
object with a simple code to update, add, delete rows in the tables.
I wanted to know, what are the advantages of using one way of querying over another?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ADO.NET 为您提供对查询的低级别控制。如果查询速度很重要,那么这就是您想要的地方。如果速度不是很重要,但快速开发和对象关系模型很重要,那么 LINQ to SQL 是一个安全的选择。
不过,我会推荐 Linq to SQL over ADO.NET。
我还会看看实体框架。与 Linq 相比,它使您能够更好地控制对象以及它们的实现、使用和处理方式。
ADO.NET gives you low level control over your queries. If query speed is going to be of importance, this is where you want to be. If you speed is not very important, but rapid development and an Object Relational Model is, LINQ to SQL is a safe bet.
I would recommend Linq to SQL over ADO.NET though.
Also I would look at Entity Framework. It gives you more control over your objects and how they are implemented, used and handled than Linq.
LINQ to SQL 是 ADO.NET 技术系列的一部分。它基于 ADO.NET 提供者模型提供的服务。因此,您可以将 LINQ to SQL 代码与现有 ADO.NET 应用程序混合,并将当前的 ADO.NET 解决方案迁移到 LINQ to SQL。下图提供了关系的高级视图。
请参阅以下内容:
ADO.NET 和 LINQ to SQL
优点LINQ 的缺点
LINQ to SQL 相对于普通存储过程的性能
LINQ-to-SQL 和存储过程
LINQ to SQL is part of the ADO.NET family of technologies. It is based on services provided by the ADO.NET provider model. You can therefore mix LINQ to SQL code with existing ADO.NET applications and migrate current ADO.NET solutions to LINQ to SQL. The following illustration provides a high-level view of the relationship.
Refer to the following:
ADO.NET and LINQ to SQL
Advantages & Disadvantages of LINQ
Performance of LINQ to SQL over Normal Stored procedure
LINQ-to-SQL and Stored Procedures
LINQ to SQL 的伟大之处在于它可以为您生成大量的管道代码。但它与直接使用 ADO.NET/SQL 基本相同。要在 LINQ to SQL 中执行更复杂的数据操作,您必须知道如何在 LINQ 中编写复杂的联接,就像在 SQL 中一样。
查看实体框架 - 它可能会为您提供您正在寻找的更高级别的抽象。
LINQ to SQL is great in that it generates alot of the plumbing code for you. But it is basically the same as using straight up ADO.NET/SQL. To do more complex data manipulation in LINQ to SQL you have to know how write complex joins in LINQ just as you would in SQL.
Look into Entity Framework - it might give you a higher level of abstraction that you are looking for.
两者处于不同的抽象级别。 ADO.NET 是.NET 中最低级别的数据访问。其他任何事情都将建立在它的基础上。
每个抽象都应该赋予您以较低级别概念为代价来表达较高级别概念的能力。
如果我听起来像个哲学家,那是因为今天是星期五。
The two are on different abstraction levels. ADO.NET is the lowest level of data access in .NET. Anything else will build upon it.
Every abstraction should give you power to express higher-level concepts at the cost of lower level concepts.
If I sound like a philosopher it's because it's Friday.
除了实体框架之外,您还可以看看 NHibernate(另一个 .net 对象关系映射器)。它比 EF 存在的时间更长,因此更成熟一些,但如果这对您很重要的话,它不是由 Microsoft 开发的。
In addition to Entity Framework, you can take a look at NHibernate (another .net Object Relational Mapper). It's been around longer than EF so it's a bit more mature, but it isn't developed by Microsoft if that matters to you.