从数据库中提取数据的方法

发布于 2024-10-08 08:47:14 字数 177 浏览 0 评论 0原文

我正准备启动一个 C# Web 应用程序项目,只是想要一些有关从数据库提取数据的意见。据我所知,我可以使用 C# 代码从 Web 应用程序的后台代码(即 LINQ)访问数据库,也可以调用一个存储过程来收集所有数据,然后用几行代码读取它我的代码后面的代码。我很想知道这两种方法或任何其他方法中的哪一种是最有效、最优雅、面向未来且最容易测试的。

I'm getting ready to start a C# web application project and just wanted some opinions regarding pulling data from a database. As far as I can tell, I can either use C# code to access the database from the code behind (i.e. LINQ) of my web app or I can call a stored procedure that will collect all the data and then read it with a few lines of code in my code behind. I'm curious to know which of these two approaches, or any other approach, would be the most efficient, elegant, future proof and easiest to test.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妄想挽回 2024-10-15 08:47:14

编写应用程序最适合未来的方法是在您和数据库之间进行抽象。为此,您需要使用某种 ORM。我建议使用 NHibernate 或实体框架。

这将为您提供只需编写一次查询而不是多次的优势(例如:如果您决定更改数据库“从 mssql 移动到 mysql,反之亦然”)。这还为您提供了将所有数据保存在对象中的优势。这比原始的 ado Datatables 或 DataReaders 更容易使用。

The most future proof way to write your application would be to have an abstraction between you and your database. To do that you would want to use an ORM of some sort. I would recommend using either NHibernate or Entity Framework.

This would give you the advantage of only having to write your queries once instead of multiple times (Example: if you decide to change your database "moving from mssql to mysql or vice versa"). This also gives you the advantage of having all of your data in objects. Which is much easier to work with than raw ado Datatables or DataReaders.

神回复 2024-10-15 08:47:14

大多数开发人员喜欢在后台代码和数据库之间引入至少一层。

此外,人们在该层中使用许多数据访问策略。 ADO.NET、实体框架、企业库 NHibernate、Linq 等。

在所有这些中,您都可以使用 SQL 查询或存储过程。我更喜欢存储过程,因为它们对我来说很容易编写和部署。其他人更喜欢使用参数化查询。

当你有如此多的选择时,通常表明确实没有明显的赢家。这意味着你可能只需选择一个方向并坚持下去就可以了。

但是您确实不应该使用非参数化查询,也不应该在后面的代码中执行此操作,而应在单独的类中执行

Most developers like to introduce at least one layer between the code behind and the Database.

Additionally there are many data access strategies that people use within that layer. ADO.NET, Entity Framework, Enterprise Library NHibernate, Linq etc.

In all of those you can use SQL Queries or Stored Procedures. I prefer Stored Procedures because they are easy for me to write and deploy. Others prefer to use Parameterized queries.

When you have so many options its usually indicative that there really isn't a clear winner. This means you can probably just pick a direction and go with it and you'll be fine.

But you really shouldn't use non-parameterized queries and you shouldn't do it in the code behind but instead in seperate classes

二手情话 2024-10-15 08:47:14
  1. 使用 LINQ to SQL 访问数据可能是目前最糟糕的选择。 Microsoft 表示他们将不再改进 LINQ to SQL 以支持实体框架。另外,如果您选择这样做,则可以将 LINQ 与 EF 结合使用。

  2. 我建议使用像 nHibernate 或实体框架这样的 ORM,而不是 sproc/ADO 方法。在这两个 ORM 之间,我可能会建议您使用 EF,因为您刚刚掌握了这一点。 EF 并不像 nHibernate 那样强大,但它的学习曲线较短并且非常强大。

  1. Using LINQ to SQL to access your data is probably the worst choice right now. Microsoft has said that they will no longer be improving LINQ to SQL in favor of Entity Framework. Also, you can use LINQ with your EF if you should choose to go that route.

  2. I would recommend using an ORM like nHibernate or Entity framework instead of a sproc/ADO approach. Between the two ORMs, I would probably suggest EF for you where you are just getting the hang of this. EF isn't QUITE as powerful as nHibernate but it has a shorter learning curve and is pretty robust.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文