今天又根据CodeSmith的几个基本组件写出了基于表生成删除功能的存储过程代码生成模板。
        昨天觉得添加的存储过程模板写的比较简单,今天准备详细介绍一下这个删除的模板。
        首先介绍我们使用到的一个教本函数GetSqlParameterStatement(ColumnSchema column),其函数代码如下:
 1CodeSmith应用(二)public string GetSqlParameterStatement(ColumnSchema column)
 2}

        大家可以看到,这个函数需要传入一个ColumnSchema类型的参数,它代表一个数据表中的列,并且是一个列,然后根据ColumnSchema这个类具有的属性,对传入的列进行一些操作然后返回我们生成存储过程时需要的代码。
        首先介绍一下ColumnSchema的一些常用属性,如下表: 

属性Property

描述Description

AllowDBNull

是否允许空值NULL

Database

通过DatabaseSchema对象得到当前列所属的数据库

DataType

此数据对象的数据类型

Description

当前对象的描述

ExtendedProperties

用来存储SchemaObject的其他附加信息

IsForeignKeyMember

当前列是否为外键

IsPrimaryKeyMember

当前列是否为主键

IsUnique

当前列是否唯一

Name

列的名称

NativeType

列定义的数据类型

Precision

数据对象的精度

Scale

数据对象的范围(个人理解为需要保留小数的范围)

Size

数据对象的大小(例如:字符串长度为10

SystemType

数据对象的系统类型

Table

当前列所属的数据表

        下面为我们首先要生成存储过程,要自动生成的代码分成了红、绿、蓝三部分。
CREATE PROCEDURE dbo.CustomersDelete
/*
==================================================
Author:Bear-Study-Hard
CreatedTime:2005-12-28
Description:Delete a record from table Customers
==================================================
*/
@CustomerID nchar(5) --客户ID
AS
Delete From [Customers]
Where
[CustomerID] = @CustomerID

    我们的这个脚本函数就是要实现拼出红色的部分,GetSqlParameterStatement函数接收ColumnSchema类型的参数后,从其Name属性和NativeType属性拼出@CustomerID nchar部分,然后由于不同的数据类型尤其是数值类型和字符串类型的区别,会导致数据类型的大小会有所不同,这里仅对Decimal的数据类型进行了判断(Numericfloat等均需要这种处理),然后根据Precision属性得到精度并通过Scale属性得到了需要保留小数的范围。如果传出的为非Decimal类型字段则直接通过Size属性取出其大小即可。得到了(5)部分。最后的注释是为了生成的存储过程解读性好加上的,使用的是Description属性。
    剩下的绿色部分和蓝色部分生成时比较简单,请各位自行学习。模板代码为:

 1CodeSmith应用(二)<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Description="Create a procedure which have delete function base on a table.Must use PrimaryKey to delete a record." %>
 2CodeSmith应用(二)<%@ Assembly Name="SchemaExplorer" %>
 3CodeSmith应用(二)<%@ Import Namespace="SchemaExplorer" %>
 4CodeSmith应用(二)<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="DataTable" Description="Table that the stored procedures should be based on." %>
 5CodeSmith应用(二)<%@ Property Name="Author" Type="String" Category="Context" Description="The author for this procedure." Optional="true"%>
 6CodeSmith应用(二)<%@ Property Name="Description" Type="String" Category="Context" Description="The description for this procedure." Optional="true"%>
 7CodeSmith应用(二)<script runat="template">
 8CodeSmith应用(二)public string GetSqlParameterStatement(ColumnSchema column)
 9CodeSmith应用(二){
10CodeSmith应用(二)    string param = "@" + column.Name + " " + column.NativeType;
11CodeSmith应用(二)    switch (column.DataType)
12CodeSmith应用(二)    {
13CodeSmith应用(二)        case DbType.Decimal:
14CodeSmith应用(二)        {
15CodeSmith应用(二)            param += "(" + column.Precision + ", " + column.Scale + ")";
16CodeSmith应用(二)            break;
17CodeSmith应用(二)        }
18CodeSmith应用(二)        default:
19CodeSmith应用(二)        {
20CodeSmith应用(二)            if (column.Size > 0)
21CodeSmith应用(二)            {
22CodeSmith应用(二)                param += "(" + column.Size + ")";
23CodeSmith应用(二)            }
24CodeSmith应用(二)            break;
25CodeSmith应用(二)        }
26CodeSmith应用(二)    }
27CodeSmith应用(二)    return param;
28CodeSmith应用(二)}
29CodeSmith应用(二)</script>
30CodeSmith应用(二)CREATE PROCEDURE dbo.<%=SourceTable.Name %>Delete
31CodeSmith应用(二)/*
32CodeSmith应用(二)==================================================
33CodeSmith应用(二)Author:<%= Author %>
34CodeSmith应用(二)CreatedTime:<%= System.DateTime.Now.ToShortDateString() %>
35CodeSmith应用(二)Description:<%= Description %>
36CodeSmith应用(二)==================================================
37CodeSmith应用(二)*/
38CodeSmith应用(二)<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
39CodeSmith应用(二)<%= GetSqlParameterStatement(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < SourceTable.PrimaryKey.MemberColumns.Count - 1) { %>,<% } %>    <% if (SourceTable.Columns[i].Description != "") { %>--<%= SourceTable.Columns[i].Description %><% } %>
40CodeSmith应用(二)<% } %>
41CodeSmith应用(二)AS
42CodeSmith应用(二)Delete From [<%= SourceTable.Name %>
43CodeSmith应用(二)Where
44CodeSmith应用(二)<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
45CodeSmith应用(二)<% if (i > 0) { %>AND <% } %>[<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>
46CodeSmith应用(二)<% } %>

相关文章:

  • 2022-02-20
  • 2021-11-24
  • 2021-09-30
猜你喜欢
  • 2021-07-02
  • 2021-09-01
  • 2021-08-23
  • 2021-07-25
相关资源
相似解决方案