来自 SQL 数据库表的 C# 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/219986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 18:39:13  来源:igfitidea点击:

C# class from a SQL database table

c#.netorm.net-2.0

提问by

Came across this:

遇到了这个:

http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp

http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp

And wondering if this is the right solution as I am not that big of a fan of creating a class for every stored procedure or do I use Enterprise Library for ASP.net 2.0 project.

并且想知道这是否是正确的解决方案,因为我不太喜欢为每个存储过程创建一个类,或者我是否将企业库用于 ASP.net 2.0 项目。

回答by alexmac

Is there any possibility of upgrading to framework 3.5? if so take a look at LINQ to SQL and Entity Framework as this will accomplish alot of this for you.

是否有可能升级到框架 3.5?如果是这样,请查看 LINQ to SQL 和实体框架,因为这将为您完成很多工作。

If not then as long as it generates standard code that doesnt tie you into 3rd party libraries then you could certainly use it. At my workplace we have our own generator similar to this and it works well although we will shortly be moving to LINQ to SQL.

如果没有,那么只要它生成的标准代码不会将您绑定到 3rd 方库,那么您当然可以使用它。在我的工作场所,我们有自己的与此类似的生成器,它运行良好,尽管我们很快将转向 LINQ to SQL。

回答by Dylan Beattie

There are many ways of wrapping a database table in a C# class; you probably want to investigate a few alternatives before choosing between the one you've linked to and the Entity Framework.

在 C# 类中包装数据库表的方法有很多种;在您链接到的选项和实体框架之间进行选择之前,您可能想要调查一些替代方案。

There's a software pattern called the "active record pattern" which describes exactly this approach - one C# class for each table, with load/save methods like Customer.GetById(), Customer.Save(), and so on.

有一种称为“活动记录模式”的软件模式准确地描述了这种方法 - 每个表一个 C# 类,具有加载/保存方法,如 Customer.GetById()、Customer.Save() 等。

For ASP.NET 2.0, check out the Castle Project's ActiveRecord implementationand a third-party Visual Studio plugin tool called ActiveWriterthat lets you generate class wrappers for your tables using a drag'n'drop interface.

对于 ASP.NET 2.0,请查看Castle Project 的 ActiveRecord 实现和名为ActiveWriter的第三方 Visual Studio 插件工具,该工具可让您使用拖放界面为表生成类包装器。

回答by Dr8k

You definitely shouldn't be creating a class for every stored procedure. There are a number of approaches you can take to handling your database interactions. You should have a good look at the major frameworks out there and decide which one best suits you. The Castle Project solution is great, and relies on nHibernate (nHibernate). LINQ is a similar offering by Mircrosoft (LINQ Project). Both of these solutions are full ORM frameworks (Object Relational Mapping) and will generate dynamic SQL to persist your objects in the database. Each also has it's own quirks and likes you to structure your objects in particular ways. If you don't want to manage the SQL your system uses, I would definitely recommend one of these approaches.

您绝对不应该为每个存储过程创建一个类。您可以采用多种方法来处理数据库交互。你应该好好看看那里的主要框架,然后决定哪一个最适合你。Castle Project 解决方案很棒,并且依赖于 nHibernate ( nHibernate)。LINQ 是 Mircrosoft ( LINQ Project)提供的类似产品。这两种解决方案都是完整的 ORM 框架(对象关系映射),并将生成动态 SQL 以将您的对象持久保存在数据库中。每个人也有自己的怪癖,并喜欢您以特定方式构建对象。如果您不想管理系统使用的 SQL,我肯定会推荐其中一种方法。

I come from a database background, and prefer a bit more control over my SQL. In particular I like to have my interractions handled by stored procedures. I find this enables me to control both the SQL better for optimisation, but helps me manage database security in a more friendly manner. To accommodate this approach, I recommend something like iBatis (iBatis). iBatis isn't a full ORM, but rather a simple SQL mapper. The downside to my approach is that you need to write a lot more code (SQL), but I don't mind the trade-off.

我来自数据库背景,更喜欢对我的 SQL 进行更多控制。我特别喜欢让我的交互由存储过程处理。我发现这使我能够更好地控制 SQL 以进行优化,但有助于我以更友好的方式管理数据库安全性。为了适应这种方法,我推荐像 iBatis ( iBatis)这样的东西。iBatis 不是一个完整的 ORM,而是一个简单的 SQL 映射器。我的方法的缺点是您需要编写更多代码 (SQL),但我不介意权衡。

回答by David Robbins

You will need to determine at what point you need sets of data that are composed from your tables, and whether you want SQL to produce these with stored procedures or if your business logic layer will handle these. As Dr8k says, nHibernate will create SQL for you, but there is a learning curve with nHibernate. The ORM will be in control of how you are getting the data and depending on your environment and DBA's conmfort level you may other issues to overcome.

您需要确定在什么时候需要由表组成的数据集,以及您是否希望 SQL 使用存储过程生成这些数据集,或者您的业务逻辑层是否将处理这些数据集。正如 Dr8k 所说,nHibernate 会为你创建 SQL,但是 nHibernate 有一个学习曲线。ORM 将控制您获取数据的方式,并且根据您的环境和 DBA 的舒适度,您可能需要克服其他问题。

If you more comfortable with SQL, then there is another tool called SubSonicthat will create wrappers ala Active Record for you while offering you the ability to use stored procedures as well. There is also a nice query tool with a fluent interface that you can use if you are not able to use LINQ.

如果您更熟悉 SQL,那么还有另一个名为SubSonic 的工具,它可以为您创建包装器 ala Active Record,同时还为您提供使用存储过程的能力。还有一个很好的查询工具,它具有流畅的界面,如果您不能使用 LINQ,您可以使用它。