I been wondering about this for a while. It seems like there are so many ways now I don't know when to use what? Or if there is even a point to learn them. Like I don't know if they basically do all the same things and just basically stick with one till you master it then maybe look at other ones.
我一直在想这个问题。好像现在有这么多方法,我不知道什么时候用什么?或者,如果甚至有一点要学习它们。就像我不知道他们是否基本上做了所有相同的事情,只是基本上坚持一个,直到你掌握它,然后可能看看其他的。
So when I was taking an ASP.NET course that was part of my program.
所以,当我参加一个属于我的课程的ASP.NET课程时。
We first did like ADO.NET where we just wrote everything with SQL statements in the code. We then moved onto a 3 tier architecture. This was done by making like classes and having datasets that return stuff.
我们首先做了ADO.NET,我们只是在代码中用SQL语句编写了所有东西。然后,我们转向了3层架构。这是通过制作类似的类并具有返回内容的数据集来完成的。
The SQL was written in the class. I personally never really liked this way since I always found it annoying with trying to get the quotes right and just overall did not like it.
SQL是在类中编写的。我个人从来没有真正喜欢这种方式,因为我总是发现它很烦人,试图得到正确的报价,只是总体上不喜欢它。
I then found on the Asp.net site their tutorial of 3 tier arch that I really liked. They where using typed data sets. You add the dataset file to you DAL folder and you would make table adapters and stuff through the GUI. You would then write you code in these GUI, I found it the perfect solution since now my SQL code was away from my code and I did not have to worry about quotes and all that stuff not being right or closing the connections and stuff plus it even had a SQL GUI builder!
然后我在Asp.net网站上找到了我真正喜欢的3层拱门教程。他们使用类型化数据集。您将数据集文件添加到DAL文件夹,您将通过GUI创建表适配器和东西。然后你会在这些GUI中编写代码,我发现它是完美的解决方案,因为现在我的SQL代码远离我的代码而且我不必担心引号和所有那些不正确的东西或关闭连接和东西加上它甚至有一个SQL GUI构建器!
Then I would just make file in the BLL folder and make a property to grab the table adapter and write my business layer logic.
然后我只需要在BLL文件夹中创建文件并创建一个属性来获取表适配器并编写我的业务层逻辑。
The only thing I did not like was since it was typed if my stuff tried to return some new rows it would get mad.
我唯一不喜欢的是因为它是键入的,如果我的东西试图返回一些新行,它会生气。
So when I had to join tables I usually had to make a new table adapter.
因此,当我不得不连接表时,我通常不得不制作一个新的表适配器。
Now there seems to be so many of them.
现在似乎有这么多。
- Linq -> what some people said would replace ADO.NET and some said it would not.
- Linq - >有些人说会取代ADO.NET,有些人说不会。
- Linq to sql
- Linq to sql
- ado.net
- ado.net
I am not sure if that is all of them probably not.
我不确定是否所有这些都可能不是。
Before writing this post I did a quick check to see what linq to sql was about and saw some posts saying that MS was killing it off. They where from like 2008 so I don't know if this is true or not but I noticed that almost all the MVC books use like linq to sql so I don't think it is.
在写这篇文章之前,我做了一个快速检查,看看linq到sql是什么,并看到一些帖子说MS正在杀掉它。他们来自2008年,所以我不知道这是不是真的,但我注意到几乎所有的MVC书都像linq一样使用sql所以我认为不是。
So is it worth switching to something a different one then typed datasets? Or are each used for different situations?
那么是否值得切换到不同的类型然后输入数据集?或者每个都用于不同的情况?
3 个解决方案
#1
21
LINQ by itself is just a base technology ("Language Integrated Query") that's baked into C# 3.0 - it has nothing to do per se with databases. LINQ can be used against a variety of things - databases, XML, objects in memory, Entity Framework entities, Active Directory - you name it.
LINQ本身只是一种基础技术(“语言集成查询”),它融入了C#3.0 - 它本身与数据库无关。 LINQ可用于各种事物 - 数据库,XML,内存中的对象,实体框架实体,Active Directory - 您可以命名它。
Linq-To-SQL is the lightweight, straightforward, MS-SQLServer only technology which allows you to use SQL Server tables easily and nicely as real objects in your .NET application. It's a "Object-relational mapper" that makes dealing with databases easier. It's SQL Server only, and Microsoft will not extend it much further - it's available, also in .NET 4.0, but won't be further developed any more.
Linq-To-SQL是一种轻量级,简单易用的MS-SQLServer技术,它允许您轻松地将SQL Server表用作.NET应用程序中的真实对象。它是一个“对象关系映射器”,可以更轻松地处理数据库。它只是SQL Server,微软不会进一步扩展它 - 它也可以在.NET 4.0中使用,但不会再进一步开发了。
ADO.NET is the base data access technology in .NET - it gives you access to a wide variety of data stores, relational and non-relational. It's the very basic technology - you deal with your data in a very low-level, raw manner.
ADO.NET是.NET中的基础数据访问技术 - 它允许您访问各种数据存储,关系和非关系。这是最基本的技术 - 您以非常低级的原始方式处理数据。
On top of that, you have the ADO.NET datasets, which are a bit like Linq-to-SQL in that they make it somewhat easier to deal with databases. Contrary to Linq-to-SQL, you're not dealing with your objects from your domain model in your .NET code, but instead you're dealing with the database-oriented rows and columns just like they exist in the database. It's a more direct representation of what's in the database, it's on a lower level, it's very tightly coupled to your database layout, and it's not as "nice" and easy to use as Linq-To-SQL objects - you deal with lowlevel rows and columns and their values.
最重要的是,您拥有ADO.NET数据集,它们有点像Linq-to-SQL,因为它们使得处理数据库变得更加容易。与Linq-to-SQL相反,您不是在.NET代码中处理域模型中的对象,而是处理面向数据库的行和列,就像它们存在于数据库中一样。它更直接地表示数据库中的内容,它位于较低级别,它与数据库布局紧密耦合,并且它不像Linq-To-SQL对象那样“好”且易于使用 - 您处理的是低级别的行和列及其值。
If you have the choice right now, and don't need anything but SQL Server, I would highly recommend checking out Linq-to-SQL - the mapping from the raw database tables to nice and easy to use .NET objects really makes your life a whole lot easier!
如果您现在可以选择,除了SQL Server之外不需要任何东西,我强烈建议您查看Linq-to-SQL - 从原始数据库表到漂亮且易于使用的.NET对象的映射真正让您的生活一件容易的事!
Marc
渣子
#2
12
To clarify the LINQ-to-SQL story; it isn't "dead" - it is very much a fully supported part of the .NET framework, and under active development (there is a LINQ-to-SQL team at Redmond).
澄清LINQ-to-SQL的故事;它不是“死” - 它是.NET框架的一个完全支持的部分,并且在积极的开发中(Redmond有一个LINQ-to-SQL团队)。
The point is that the new feature development is mainly going into EF, including (hopefully) bridging the gap between LINQ-to-SQL (which is generally very popular) and EF (which has been criticised in a lot of ways).
关键是新功能开发主要是进入EF,包括(希望)弥合LINQ-to-SQL(通常非常流行)和EF(在很多方面受到批评)之间的差距。
For example, EF in 4.0 supports POCO objects, like LINQ-to-SQL.
例如,4.0 in中的EF支持POCO对象,如LINQ-to-SQL。
Personally, I'm still a fan of LINQ-to-SQL, and would happily use it for new builds - but I would hide all of this behind a repository interface so that I can swap it at will, to any tool:
就个人而言,我仍然是LINQ-to-SQL的粉丝,并乐意将其用于新版本 - 但我会将所有这些隐藏在存储库界面后面,以便我可以随意交换任何工具:
- raw ADO.NET
- 原始ADO.NET
- LINQ-to-SQL
- LINQ到SQL
- NHibernate
- NHibernate的
- LLBLGenPro
- LLBLGenPro
- Entity Framework
- 实体框架
The one thing I wouldn't touch is DataSet
;-p
我不接触的一件事是DataSet ;-p
#3
4
I'd say that if you're having trouble with getting quotes right in your SQL statements using ADO.NET, you're probably building the SQL statements in the wrong way. Prone to SQL injection or at the very least: messy code.
我会说,如果你在使用ADO.NET在SQL语句中获得引号时遇到问题,那么你可能正在以错误的方式构建SQL语句。容易出现SQL注入或至少:凌乱的代码。
LINQ to SQL uses ADO.NET to do it's thing. It's a combination of an Object-relational mapping (ORM) tool combined with a new query syntax basically. I wouldn't invest time in LINQ to SQL as Microsoft has declared it end of life.
LINQ to SQL使用ADO.NET来做这件事。它基本上是对象关系映射(ORM)工具与新查询语法相结合的组合。我不会把时间花在LINQ to SQL上,因为微软宣称它已经过时了。
Entity Framework is the LINQ to SQL replacement. It is an ORM tool which comes with it's own SQL like query language, Entity SQL. You can use LINQ on top of it, so that most simple queries are exactly the same as in LINQ to SQL.
实体框架是LINQ to SQL的替代品。它是一个ORM工具,它带有自己的SQL,如查询语言,Entity SQL。您可以在其上使用LINQ,以便大多数简单查询与LINQ to SQL中的完全相同。
What method to use depends largely on what you're trying to do. I would not use typed DataSets (or any DataSets really). It's a personal preference I guess, but if you can create typed DataSets, you're better off doing full scale object-relational mapping.
使用什么方法很大程度上取决于你想要做什么。我不会使用类型化的DataSet(或任何DataSet)。我猜这是个人偏好,但是如果你可以创建类型化的DataSet,那么你最好做全面的对象关系映射。
Knowing basic ADO.NET is a useful skill however you look at it though. Especially if you need to update multiple records in a database without first retrieving that data, you'll always end up writing SQL statements. I'd recommend creating stored procedures for those cases, which you can call with plain ADO.NET, or you can you can add them to your model in Entity Framework and call them through there.
知道基本的ADO.NET是一项很有用的技巧,但是你看一下它。特别是如果您需要在不首先检索数据的情况下更新数据库中的多个记录,您将始终最终编写SQL语句。我建议为这些案例创建存储过程,您可以使用普通的ADO.NET调用它们,或者您可以将它们添加到Entity Framework中的模型并通过那里调用它们。
Entity Framework gives you some database independence (unlike LINQ to SQL). There are implementations for Oracle around, but I have no experience with them personally. From what I've heard they impose some undesired limitations if you're doing semi-complicated work.
实体框架为您提供了一些数据库独立性(与LINQ to SQL不同)。有围绕Oracle的实现,但我个人没有经验。据我所知,如果你做半复杂的工作,他们会施加一些不受欢迎的限制。
#1
21
LINQ by itself is just a base technology ("Language Integrated Query") that's baked into C# 3.0 - it has nothing to do per se with databases. LINQ can be used against a variety of things - databases, XML, objects in memory, Entity Framework entities, Active Directory - you name it.
LINQ本身只是一种基础技术(“语言集成查询”),它融入了C#3.0 - 它本身与数据库无关。 LINQ可用于各种事物 - 数据库,XML,内存中的对象,实体框架实体,Active Directory - 您可以命名它。
Linq-To-SQL is the lightweight, straightforward, MS-SQLServer only technology which allows you to use SQL Server tables easily and nicely as real objects in your .NET application. It's a "Object-relational mapper" that makes dealing with databases easier. It's SQL Server only, and Microsoft will not extend it much further - it's available, also in .NET 4.0, but won't be further developed any more.
Linq-To-SQL是一种轻量级,简单易用的MS-SQLServer技术,它允许您轻松地将SQL Server表用作.NET应用程序中的真实对象。它是一个“对象关系映射器”,可以更轻松地处理数据库。它只是SQL Server,微软不会进一步扩展它 - 它也可以在.NET 4.0中使用,但不会再进一步开发了。
ADO.NET is the base data access technology in .NET - it gives you access to a wide variety of data stores, relational and non-relational. It's the very basic technology - you deal with your data in a very low-level, raw manner.
ADO.NET是.NET中的基础数据访问技术 - 它允许您访问各种数据存储,关系和非关系。这是最基本的技术 - 您以非常低级的原始方式处理数据。
On top of that, you have the ADO.NET datasets, which are a bit like Linq-to-SQL in that they make it somewhat easier to deal with databases. Contrary to Linq-to-SQL, you're not dealing with your objects from your domain model in your .NET code, but instead you're dealing with the database-oriented rows and columns just like they exist in the database. It's a more direct representation of what's in the database, it's on a lower level, it's very tightly coupled to your database layout, and it's not as "nice" and easy to use as Linq-To-SQL objects - you deal with lowlevel rows and columns and their values.
最重要的是,您拥有ADO.NET数据集,它们有点像Linq-to-SQL,因为它们使得处理数据库变得更加容易。与Linq-to-SQL相反,您不是在.NET代码中处理域模型中的对象,而是处理面向数据库的行和列,就像它们存在于数据库中一样。它更直接地表示数据库中的内容,它位于较低级别,它与数据库布局紧密耦合,并且它不像Linq-To-SQL对象那样“好”且易于使用 - 您处理的是低级别的行和列及其值。
If you have the choice right now, and don't need anything but SQL Server, I would highly recommend checking out Linq-to-SQL - the mapping from the raw database tables to nice and easy to use .NET objects really makes your life a whole lot easier!
如果您现在可以选择,除了SQL Server之外不需要任何东西,我强烈建议您查看Linq-to-SQL - 从原始数据库表到漂亮且易于使用的.NET对象的映射真正让您的生活一件容易的事!
Marc
渣子
#2
12
To clarify the LINQ-to-SQL story; it isn't "dead" - it is very much a fully supported part of the .NET framework, and under active development (there is a LINQ-to-SQL team at Redmond).
澄清LINQ-to-SQL的故事;它不是“死” - 它是.NET框架的一个完全支持的部分,并且在积极的开发中(Redmond有一个LINQ-to-SQL团队)。
The point is that the new feature development is mainly going into EF, including (hopefully) bridging the gap between LINQ-to-SQL (which is generally very popular) and EF (which has been criticised in a lot of ways).
关键是新功能开发主要是进入EF,包括(希望)弥合LINQ-to-SQL(通常非常流行)和EF(在很多方面受到批评)之间的差距。
For example, EF in 4.0 supports POCO objects, like LINQ-to-SQL.
例如,4.0 in中的EF支持POCO对象,如LINQ-to-SQL。
Personally, I'm still a fan of LINQ-to-SQL, and would happily use it for new builds - but I would hide all of this behind a repository interface so that I can swap it at will, to any tool:
就个人而言,我仍然是LINQ-to-SQL的粉丝,并乐意将其用于新版本 - 但我会将所有这些隐藏在存储库界面后面,以便我可以随意交换任何工具:
- raw ADO.NET
- 原始ADO.NET
- LINQ-to-SQL
- LINQ到SQL
- NHibernate
- NHibernate的
- LLBLGenPro
- LLBLGenPro
- Entity Framework
- 实体框架
The one thing I wouldn't touch is DataSet
;-p
我不接触的一件事是DataSet ;-p
#3
4
I'd say that if you're having trouble with getting quotes right in your SQL statements using ADO.NET, you're probably building the SQL statements in the wrong way. Prone to SQL injection or at the very least: messy code.
我会说,如果你在使用ADO.NET在SQL语句中获得引号时遇到问题,那么你可能正在以错误的方式构建SQL语句。容易出现SQL注入或至少:凌乱的代码。
LINQ to SQL uses ADO.NET to do it's thing. It's a combination of an Object-relational mapping (ORM) tool combined with a new query syntax basically. I wouldn't invest time in LINQ to SQL as Microsoft has declared it end of life.
LINQ to SQL使用ADO.NET来做这件事。它基本上是对象关系映射(ORM)工具与新查询语法相结合的组合。我不会把时间花在LINQ to SQL上,因为微软宣称它已经过时了。
Entity Framework is the LINQ to SQL replacement. It is an ORM tool which comes with it's own SQL like query language, Entity SQL. You can use LINQ on top of it, so that most simple queries are exactly the same as in LINQ to SQL.
实体框架是LINQ to SQL的替代品。它是一个ORM工具,它带有自己的SQL,如查询语言,Entity SQL。您可以在其上使用LINQ,以便大多数简单查询与LINQ to SQL中的完全相同。
What method to use depends largely on what you're trying to do. I would not use typed DataSets (or any DataSets really). It's a personal preference I guess, but if you can create typed DataSets, you're better off doing full scale object-relational mapping.
使用什么方法很大程度上取决于你想要做什么。我不会使用类型化的DataSet(或任何DataSet)。我猜这是个人偏好,但是如果你可以创建类型化的DataSet,那么你最好做全面的对象关系映射。
Knowing basic ADO.NET is a useful skill however you look at it though. Especially if you need to update multiple records in a database without first retrieving that data, you'll always end up writing SQL statements. I'd recommend creating stored procedures for those cases, which you can call with plain ADO.NET, or you can you can add them to your model in Entity Framework and call them through there.
知道基本的ADO.NET是一项很有用的技巧,但是你看一下它。特别是如果您需要在不首先检索数据的情况下更新数据库中的多个记录,您将始终最终编写SQL语句。我建议为这些案例创建存储过程,您可以使用普通的ADO.NET调用它们,或者您可以将它们添加到Entity Framework中的模型并通过那里调用它们。
Entity Framework gives you some database independence (unlike LINQ to SQL). There are implementations for Oracle around, but I have no experience with them personally. From what I've heard they impose some undesired limitations if you're doing semi-complicated work.
实体框架为您提供了一些数据库独立性(与LINQ to SQL不同)。有围绕Oracle的实现,但我个人没有经验。据我所知,如果你做半复杂的工作,他们会施加一些不受欢迎的限制。