围绕存储过程自动创建 C# 包装类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/289221/
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 21:48:33  来源:igfitidea点击:

Automatically create C# wrapper classes around stored procedures

c#sql-serverstored-proceduresorm

提问by Aheho

I've inherited a rather large application that really could use some cleanup. There is data access code littered throughout the application. In code behinds, some in business logic classes, some inline in in classic asp pages.

我继承了一个相当大的应用程序,它确实可以使用一些清理。在整个应用程序中散落着数据访问代码。在代码隐藏中,一些在业务逻辑类中,一些在经典的 asp 页面中内联。

What I'd like to do is refactor this code, removing all the data access code into a few DAL classes.

我想做的是重构这段代码,将所有数据访问代码删除到几个 DAL 类中。

All the data access is done through stored procedures (Microsoft SQL 2000). There are 300-400 of them.

所有数据访问都是通过存储过程(Microsoft SQL 2000)完成的。其中有 300-400 个。

It seems like there should be an automated way to analyse the store procedures, and automatically generate c# methods for each of them, mapping the Method parameters to the stored procedure parameters, with a method returning a datatable.

似乎应该有一种自动化的方法来分析存储过程,并为每个方法自动生成 c# 方法,将方法参数映射到存储过程参数,方法返回数据表。

I don't have any experience with ORM products, and I'm not sure if what I'm looking for is a full blown ORM, or just a 3rd party utility that will help generate simple wrappers around the sp calls.

我对 ORM 产品没有任何经验,我不确定我正在寻找的是一个完整的 ORM,还是只是一个有助于围绕 sp 调用生成简单包装器的 3rd 方实用程序。

采纳答案by CMS

If you have access to .NET Framework 3.5 and Linq to SQL, you can do it very easily, check this video:

如果您可以访问 .NET Framework 3.5 和 Linq to SQL,则可以非常轻松地完成,请查看此视频:

LINQ to SQL: Using Stored Procedures

LINQ to SQL:使用存储过程

Using existing stored procedures and functions is easy with LINQ. We simply drag the stored procedures onto the O/R mapping surface and call them from the generated Data Context object.

使用 LINQ 可以轻松使用现有的存储过程和函数。我们只需将存储过程拖到 O/R 映射表面上,并从生成的数据上下文对象中调用它们。

回答by Robert Wagner

I recommend you get a hold of Code Smith. The product includes a template for ORM and fully supports generating classes from DB Schemas (and I think Procs). You can then Code Gen all the objects you need.

我建议你掌握 Code Smith。该产品包括一个 ORM 模板,并完全支持从 DB Schemas(我认为是 Procs)生成类。然后您可以 Code Gen 生成您需要的所有对象。

Another option would be to use LINQ to SQL.

另一种选择是使用 LINQ to SQL。

回答by Steven A. Lowe

dragging and dropping the stored procedures onto a dataset design surface (in .net 2.0 and higher) generates a wrapping function

将存储过程拖放到数据集设计图面(在 .net 2.0 及更高版本中)生成包装函数

but if you have a lot of them to do, you might be better off using or writing a simple code generator

但是如果你有很多事情要做,你最好使用或编写一个简单的代码生成器

two options for this:

两个选项:

  1. generate the wrapping code yourself as C# classes/methods
  2. generate a dataset.xsd file then open it in visual studio and let the designer generate the classes/methods for you
  1. 自己生成包装代码作为 C# 类/方法
  2. 生成一个 dataset.xsd 文件,然后在 Visual Studio 中打开它,让设计器为您生成类/方法

the latter can be maintained via the dataset design surface, but may be tricky to get generated right (the first time)

后者可以通过数据集设计图面进行维护,但正确生成可能很棘手(第一次)

回答by Klathzazt

My approach would be to think higher level first- create your data access classes and methods the best way you can to fit your needs for your existing or new code base. Then, use the existing procedure calls for your new objects.

我的方法是首先考虑更高层次的问题——以最好的方式创建数据访问类和方法,以满足您对现有或新代码库的需求。然后,对新对象使用现有过程调用。

I do not think you should consider any form of mass automation for this task.

我认为您不应该为此任务考虑任何形式的大规模自动化。

回答by Christian Payne

Similar to Robert's suggestion, we've written our own version of Code Smith.

与 Robert 的建议类似,我们已经编写了自己的 Code Smith 版本。

Our "Code Generator" has two parts: SQL & Classes.

我们的“代码生成器”有两部分:SQL 和类。

SQL: Will generate the Update, Select & Delete stored procs.

SQL:将生成更新、选择和删除存储过程。

C#: Will generate the classes and save the file as a .cs

C#:将生成类并将文件保存为 .cs

We call: sp_MShelpcolumns 'tablename' to get a list of fields and data types and then do a replace.

我们调用: sp_MShelpcolumns ' tablename' 来获取字段和数据类型的列表,然后进行替换。

Its not a perfect solution, but is very effective to getting the first 80% completed

它不是一个完美的解决方案,但对于完成前 80% 非常有效

回答by jeff

Not for sure, but seems like this is exactly what you were asking for.

不确定,但似乎这正是您所要求的。

http://www.codeproject.com/KB/database/SPGenerator.aspx

http://www.codeproject.com/KB/database/SPGenerator.aspx