ASP.net C# 数据库连接陷入混乱

发布于 2024-10-14 20:35:16 字数 154 浏览 7 评论 0原文

我刚刚开始在一个新网站上进行开发,并试图正确地完成它,也就是说,使用 DAL 而不是使用我之前在代码中进行的查询。

我了解这一切的原理,但我发现这个带有表适配器和向导的 DAL 功能太多,使用起来非常混乱。有没有人对我应该使用什么有任何建议,以及有关如何快速使用它的教程的链接?

I'm just starting development on a new website and am trying to do it correctly, that is, with a DAL and not with queries in code which I had before.

I understand the principles of it all, but I am finding this DAL with table adapters and wizards that do too much to be really messy to work with. Does anyone have any recommendations on what I should be using instead and a link to a tutorial on how to get going quickly with it?

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

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

发布评论

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

评论(5

心凉 2024-10-21 20:35:16

我有一个代码生成工具,已发布在我的博客上(包括源代码)。
数据访问层 CodeGen
本质上,它会为您生成大部分 DAL 代码。这是 100% 的“获取”代码以及插入、更新、删除存储过程的命令参数和赋值等。

代码生成使用 ADO.NET Core(仅此而已)。您只需查看代码即可了解应该如何编写代码(作为学习工具)。

我希望它有帮助。

I have a code generation tool that I've posted on my blog (including the source code).
Data Access Layer CodeGen
Essentially it will produces the large portion of the DAL code for you. That is 100% of the code for "Gets" and the command parameters and assignments etc. for Insert, Update, Delete stored procedures.

The code generation uses ADO.NET Core (and nothing else). You can just take a look at the code so you get a sense of how you should write your code (as a learning tool).

I hope it helps.

忘你却要生生世世 2024-10-21 20:35:16

构建自己的应用程序是有用的练习 - 它会让您了解所涉及的内容。

作为第一步,为什么不结合 Codesmith 和一些用于执行 CRUD 操作的常见数据库例程来滚动您自己的实体和数据访问呢?如果您的应用程序规模不大,这可能就是您所需要的。

继续前进,LINQ 可能会成为未来处理这些东西的方式,但当然是特定于语言的。

Building your own is useful exercise - it'll give you an awareness of what is involved.

As a first pass, why not roll your own entities and data access with a combination of Codesmith and some common DB routines for doing CRUD operations? If your app isn't massive, this will probably be all you need.

Moving on, LINQ is probably going to be the way to go with this stuff in future, but of course is language-specific.

任性一次 2024-10-21 20:35:16

我建议您考虑轻量级且易于使用的 ORM,例如 Linq to SQL。我工作的公司是一家价值 2.5B 美元的太阳能制造公司,我们使用 L2S 作为下一代制造应用程序的基础。它允许我们在 Linq 中执行所有查询(强类型查询 - 巨大的好处),并且非常容易使用。

I would suggest you consider a lightweight and easy to use ORM such as Linq to SQL. The company I work for is a $2.5B solar manufacturing company and we use L2S as the foundation for the next generation of our manufacturing applications. It allows us to do all our queries in Linq (strongly type queries - huge benefit) and it's very easy to work with.

病毒体 2024-10-21 20:35:16

就像所有其他事情一样:保持简单。如果您想自己编写DAL,那么只需编写执行数据库操作所需的代码即可。

public class MyDal
{
  private SqlConnection _connection;
  public MyDal() 
  {
    _connection = new SqlConnection("connection string here");
} public DataSet GetSomeData() { // write the code that reads from the database and parses the data in a DataSet } }

Of course, if you want to, there are numerous good solutions that prevent you from having to write your own DAL. Personally, I like Castle ActiveRecord, since it's that simple. You just write your data objects and give them some attributes, and ActiveRecord takes care of all database communication.

如果您正在从事更大的项目,您可能会研究其他解决方案、ORM,例如 Microsoft Entity Framework (EF) 等。

Just as with all other things: keep it simple. If you want to write a DAL yourself, then only write the code you need to perform database operations.

public class MyDal
{
  private SqlConnection _connection;
  public MyDal() 
  {
    _connection = new SqlConnection("connection string here");
} public DataSet GetSomeData() { // write the code that reads from the database and parses the data in a DataSet } }

Of course, if you want to, there are numerous good solutions that prevent you from having to write your own DAL. Personally, I like Castle ActiveRecord, since it's that simple. You just write your data objects and give them some attributes, and ActiveRecord takes care of all database communication.

If you are working on bigger projects, you might investigate in other solutions, ORMs, like Microsoft Entity Framework (EF), etc.

不回头走下去 2024-10-21 20:35:16

如果您可以选择,我建议您使用 .NET 4.0 并考虑使用 WebMatrix 构建您的网站 查看 Rob Conery 发表的对 WebMatrix 的思考帖子有关使用 WebMatix 及其采用的数据访问策略的更多详细信息。

If you have the option, I would suggest that you go with .NET 4.0 and look at building your site using WebMatrix Check out the Thoughts on WebMatrix post by Rob Conery for more details on using WebMatix and the Data Access strategy that it employs.

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