65.9K
CodeProject 正在变化。 阅读更多。
Home

SQL Server 存储过程生成器和 .NET C# 包装器作为单层 DAL

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.80/5 (4投票s)

2008年5月31日

CPOL

5分钟阅读

viewsIcon

38258

downloadIcon

1131

本文展示了为SQL表和C# DAL创建存储过程的通用代码

引言

我一直在寻找一个非常简单的包装器生成器,它能让我为我的 SQL 2005 表生成 C# 代码。同时,我希望能够灵活地根据各种参数创建各种 select 查询。然而,我找不到我想要的确切内容。我还希望可选地支持事务。这促使我创建了这段代码。我希望您能使用这段代码为自己生成简单的包装器。

面试流程

包装器随本文一起作为 sp_wrapper.zip 附件提供,包装器使用示例作为 adventureworks.zip 附件提供。您需要在您的 Microsoft SQL 2005 adventureworks 数据库上运行 SQL 脚本(存储在 app_code\DAL 中)。

生成器创建:生成器为每个表创建 2 个类,即 Table 类和 Controller 类。table 类的一个实例代表表的一行。List<> 用于表示行集合。Controller 允许您执行 selectinsertupdate delete

List<TableName> myrowlist1;
TableNameController mycontroller = new TableNameController();
myrowlist = mycontroller.SelectAll();

就是这样。生成器默认创建 2 个 select 查询。

  1. SelectAll
  2. Select(PrimaryKey)Select(PrimaryKey1, primarykey2)

您可以根据您的其他列或列组合另外生成 select 查询,如下所示:

SelectByColumn1Column2(column1value, column2value)

Update insert

int count = mycontroller.Update(TableNameObject, UseTransaction, sqlTransaction);
int count = mycontroller.Insert(TableNameObject, UseTransaction, sqlTransaction);
int count = mycontroller.Delete(TableNameObject, UseTransaction, sqlTransaction);
int count = mycontroller.InsertUpdate
		(TableNameObjectList, UseTransaction, sqlTransaction);

错误将作为 -1 返回。Controller 还暴露了异常源和消息。

string source = mycontroller.ErrorSource;
string message = mycontroller.ErrorMessage;

但是,如果您调用了 InsertUpdate,则更新/插入中的错误检查如下:

mycontroller.InsertUpdateErrorList //this is of type List<ErrorObj>

ErrorObj 是一个 struct,定义如下:

struct ErrorObj
{
   Object obj; //this is the row InsertUpdate was trying to update/insert
   string source;
   string message;
}

然而,mycontroller.InsertUpdate() 仍然返回一个整数,显示更新的记录数,如果函数内部发生异常则返回 -1。您需要同时检查 -1 InsertUpdateErrorList。如果所有 update/insert 都成功,InsertUpdateErrorList 将为 null

可选地,生成的代码也将支持事务。InsertUpdate Delete 函数具有函数参数:UseTransaction (bool)SqlTransaction

SqlTransaction myTransaction;
TableNameObject obj = new TableNameObject(constructors...);
TableNameObjectController cntrl = new TableNameObjectController();
int count = cntrl.Insert(obj, true, myTransaction)
//do other inserts, updates using same transaction
//int count = cntrl.Insert(obj, false, null) if not using transaction
if(count != -1)
   myTransaction.Rollback;
else
   myTransaction.Commit;

您可以查看 adventureworks.zip 文件。DAL 文件夹中的代码是 100% 生成的。

代码解析...

有两个主要的类完成了所有工作。我将从展示如何使用这两个类来围绕它们创建一个应用程序以生成代码开始。我已经在这个解决方案中创建了一个小型 Windows 应用程序,展示了如何使用这两个类来生成包装器。请注意,ErrorObj struct 目前在此应用程序中定义(GenSPWrapper.cs)。

我将简要介绍一下这两个类如何直接使用,然后我将展示如何使用我在本解决方案中创建的 Windows 窗体。

  1. CStoredProcedures.cs - 此类根据构造函数中提供的表信息生成 SQL 脚本。
    public CStoredProcedures(DataTable columnSchemaDt, 
    	string tableName, string tableOwner, string[] whereArray)
  2. CWrapper.cs - 此类生成 C# 代码。下面是您提供初始信息的构造函数。
    public CWrapper(string cnnString, string tablename, 
    	string projnamespace, DataTable schemaDt, string[] whereArray)
  • columnSchemaDt/schemaDt 是您从 DataReader 获取的架构 datatable。请参阅解决方案中的示例。
  • tableName 是数据库中表的名称。
  • tableOwner tableowner,通常是 dbo
  • projnamespace - 生成代码的命名空间。
  • cnnString - 这是在 Controller 中使用的 string。您也可以在此处添加类似以下的语句:
    cnnString = "ConfigurationSettings.ConnectionStrings[0].ConnectionString" or
    cnnString = "\"Data Source=.;
    Initial Catalog=adventureworks;Integrated Security=True\"" 
    //notice how double quotes are placed. 
  • string[] whereArray - 默认情况下,该对象仅生成 2 个 select 语句,一个用于 select all ,另一个用于使用主键进行 select。如果您需要带有其他列的 WHERE 表达式的 select 语句,则需要在此 whereArray中提供所有这些组合。
  • whereArray 格式:以 '|'(不带引号)分隔,第一个元素是表名,然后是列。string[] 中的每个 string 代表一个组合。
  • 示例:为了向您展示一个示例,我假设数据库中有以下表:
    • MyTable
    • OrderNo (主键)、CustomerIDStatusAmount

现在,我知道生成器将生成 SelectAll Select(OrderNo)

我想要另外 2 个 select

  1. SelectByCustomerID(int customerID)
  2. SeelctByCustomerIDStatus(int customerID, int status) //我只是编造了数据类型

为了生成这两个 select 函数,我将在 whereArray中添加以下 2 个 string

string [] whereArray = new String[2];
whereArray[0] = "MyTable|CustomerID";
whereArray[1] = "MyTable|CustomerID|Status";

使用应用程序 WinForm GenSPWrapper.cs

一旦您执行了表单,您需要做的第一件事就是更新表单设置。您可以通过单击“设置”菜单来完成此操作。

wrappersettings.JPG

所有四个文本框都非常直观。更新后,关闭表单将触发应用程序重新启动。确保再次进入设置菜单(如果您是从 Visual Studio 运行应用程序),或者关闭应用程序并从 Visual Studio 重新运行。

更新后,第一个表单显示您在设置中指定的数据库中的表列表。

MainScreen.JPG

使用鼠标 + shift/control 选择要为其创建代码的表。然后单击“下一步”按钮。请帮我个忙,忽略这些标签页...我还没有打磨过这个应用程序。

您看到的下一个屏幕是:

Screen2.JPG

左侧的 listview 显示您在第一个屏幕中选择的表列表。单击任何表将在中间的 listbox中显示该表的列。在这里,您可以为“SelectBy”选择列并创建 whereArray。完成后,单击“生成”,就完成了。

我将 CWrapper CStoredProcedures 的理解留给读者……在阅读完本文后,这两个类都应该很容易理解。更改这两个类并根据您的实际需求调整代码非常容易……

欢迎提出任何问题、评论和建议……

历史

  • 2008 年 5 月 31 日:初次发布
© . All rights reserved.