我应该使用桌子适配器吗?
我正在开发一个个人项目,作为更多了解 C# 和 .NET 的一种方式,特别是创建一个利用数据库的应用程序(在我的例子中是 MS SQL Server 2008)。虽然我意识到并不总是有一种明确的“正确”的做事方式,但考虑到在使用旧技术/想法还是新技术/想法之间进行选择,我宁愿使用新技术/想法。例如,我这个项目的目标之一是学习或至少熟悉 WPF,而不是像我过去那样使用 WinForms。
在此基础上,我一直在摸索,没有太多关于将数据保存到数据库并检索它的指导。到目前为止,我已经成功地使用 TableAdapters
让这两个工作正常进行,但我觉得它们是“旧”的工作方式(我这样做的依据是它们列在 MSDN 上的 Visual Studio 2005 下) 。首先,我的这个假设正确吗?如果是这样,从数据库保存和检索数据的新方法是什么?我将不胜感激每种方法提供的任何优点和缺点。
我已经在 Google 和 MSDN 上进行了广泛的搜索,但我觉得我没有使用正确的搜索术语,因为我只是成功地让自己感到困惑。
我可以使用 .NET 3.5、Visual Studio 2008 和 Microsoft SQL Server 2008。
任何指导将不胜感激。
I am working on a personal project as a way of learning more about C# and .NET, specifically creating an application that utilises a database (MS SQL Server 2008 in my case). Whilst I appreciate there isn't always a definitive "right" way of doing things, given the choice between using an old technology/idea or a new one, I would rather use the new one. For example, one of my aims for this project is to learn, or at least familiarise myself with, WPF rather than using WinForms as I have done in the past.
On that basis, I've been muddling around without a great deal of direction with regards to saving data to my database and retrieving it. So far I've managed to get both those working using TableAdapters
but I feel like they are the "old" way of working (my basis for this is that they are listed under Visual Studio 2005 on MSDN). Firstly, am I correct in this assumption? If so, what are the newer methods of saving and retrieving data from a database? I would be grateful of any pros and cons each method offers.
I've Googled and searched MSDN extensively but I don't feel like I am using the correct search terms as I have only succeeded in confusing myself.
I have .NET 3.5, Visual Studio 2008 and Microsoft SQL Server 2008 at my disposal.
Any guidance would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我同意 TableAdapter、DataSet、DataTable 等是“旧”的处理方式。
“新”方式是 Linq-to-SQL、Entity Framework 或 NHibernate。
就我个人而言,我喜欢在需要时将 Linq-to-SQL 与普通的旧式 DBConnection、DataReader 和 DTO 结合使用。
I would agree that TableAdapters, DataSets, DataTables, etc. are the "old" way of doing things.
The "new" way would be Linq-to-SQL, Entity Framework or NHibernate.
Personally, I like to use a combination of Linq-to-SQL along with plain old
DBConnection
s,DataReader
s and DTO's where needed.如果您想要一种在 .NET 中进行数据库访问的更新方法,我建议您研究 LINQ to SQL 或实体框架。
If you would like a newer way of doing Database access in .NET, I would recommend looking into LINQ to SQL or the Entity Framework.
有许多许多不同的方法从 SQL Server 2008 检索数据使用.Net。
表适配器是一个不错的方法;它们是 .Net Framework 的核心,易于上手且相当强大,尽管它们的性能不如其他选项,并且通常需要更多内存。
There are many many many different ways to retrieve data from SQL Server 2008 using .Net.
Table Adapters are not a bad way; they are core to the .Net Framework, easy to get started with and reasonably powerful, although they do not perform quite as well as other options and often require more memory.
基本上,如果您的数据按照您想要的方式构建,那么表适配器就很好。如果您想以与存储数据不同的方式查看数据,您可以使用表适配器来完成此操作,但您失去了将更改写回数据库的能力,如果您只是生成报告,那么这是可以的。
如果您想要查看和更改数据,并且数据不在您想要查看的结构中,那么您需要实体框架,以便您可以查询数据以将其转换为不同的格式,并且仍然能够写回任何更改。这就是调用从服务器MV到显示VM的数据
Basically Table adapters are good if your data is structured the way you want to view it. If you want to view data in a different way to it is stored you can do this with a table adapter but you loose the ability to write back changes to the database, this is OK if you are just generating a report.
If you want to view and change the data and the data is not in the structure you want to view it you need entity framework so you can query the data to get it into a different format and still have the ability to write any changes back. This is what the call the data from the server the MV to the display the VM