分享
 
 
 

如何用CodeSmith减少代码重复编写

王朝asp·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

上次简单介绍了下CodeSmith,今天做些详细介绍(转),希望可以对大家有帮助:

CodeSmith 是一种语法类似于asp.net的基于模板的代码生成器,程序可以自定义模板,从而减少重复编码的劳动量,提高效率。

安装CodeSmith 2.6注册后发现有两个可运行程序CodeSmith Studio.exe和CodeSmith Explorer.exe

CodeSmith Studio.exe用来创建自定义模板

CodeSmith Explorer.exe用来导入模板并且生成代码

打开 CodeSmith Studio.exe,新建一个C#模板。发现有如下类似与asp.net的标识符号

<% %>

<%= %>

<%@ %>

<script runat="template"> </script>

下面通过简单的例子说明如何用 CodeSmith 创建模板并生成代码

新建一个空的txt文件,在文件上部输入如下一个CodeTemplate指示,Language和TargetLanguage分别代表模板语言和创建代码语言,

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a simple class " %>

然后声明几个变量,用来为了能够在以后生成的代码嵌入,这里声明了三个名为NameSpace,ClassName,Contxt的变量。其他参数一目了然就不再说明了,

<%@ Property Name="NameSpace" Type="String"

Category="Context"

Description="The namespace to use for this class" %>

<%@ Property Name="ClassName" Type="String"

Category="Context"

Description="The name of the class to generate" %>

<%@ Property Name="DevelopersName" Type="String"

Category="Context"

Description="The name to include in the comment header" %>

接下来建立将要生成代码的框架,在适当位置引用刚刚声明的变量名

using System;

namespace <%=NameSpace %>

{

/// <summary>

/// Summary description for <%=ClassName %>.

/// </summary>

public class <%=ClassName %>

{

public <%=ClassName %>()

{

}

}

}

最后,打开CodeSmith Explorer.exe,加载此模板,并且在属性对话框中任意更改声明的变量名,按Generate按钮生成合适的代码

以上简单的说明了CodeSmith的功能

下面看看在数据库访问中它是如何最小化我们的工作的

在这里我们要用到CodeSmith API中一个叫SchemaExplorer的组件,它提供了一系列类来操作数据库的框架,我们可以用它来创建表和存储过程,得到字段类型,字段名等。

如下是更新NorthWind数据库中orders表记录的存储过程,我们来看看如何自动生成它

CREATE PROCEDURE dbo.UpdateOrders

@OrderID int,

@CustomerID nchar(5),

@EmployeeID int,

@OrderDate datetime,

@RequiredDate datetime,

@ShippedDate datetime,

@ShipVia int,

@Freight money,

@ShipName nvarchar(40),

@ShipAddress nvarchar(60),

@ShipCity nvarchar(15),

@ShipRegion nvarchar(15),

@ShipPostalCode nvarchar(10),

@ShipCountry nvarchar(15)

AS

UPDATE [Orders] SET

[CustomerID] = @CustomerID,

[EmployeeID] = @EmployeeID,

[OrderDate] = @OrderDate,

[RequiredDate] = @RequiredDate,

[ShippedDate] = @ShippedDate,

[ShipVia] = @ShipVia,

[Freight] = @Freight,

[ShipName] = @ShipName,

[ShipAddress] = @ShipAddress,

[ShipCity] = @ShipCity,

[ShipRegion] = @ShipRegion,

[ShipPostalCode] = @ShipPostalCode,

[ShipCountry] = @ShipCountry

WHERE

[OrderID] = @OrderID

第一步还是创建一个CodeTemplate指示,注意TargetLanguage属性改为了T-SQL,因为创建的SQL语言代码

<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Description="Generates a update stored procedure." %>

然后加载SchemaExplorer组件,并导入SchemaExplorer命名空间,看这里是不是和asp.net很像

<%@ Assembly Name="SchemaExplorer" %>

<%@ Import Namespace="SchemaExplorer" %>

接下来声明变量,因为要从数据库表中读取框架所以 Type 属性为SchemaExplorer.TableSchema

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the stored procedures should be based on." %>

下面就要写实际将要输出代码部分的模板了。

先写存储过程的第一行 ,<%%>内为引用前面声明变量名,代表表名

CREATE PROCEDURE dbo.Update<%=SourceTable.Name %>

第二步为存储过程创造将要声明的参数列表

//开始循环遍历每列

<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>

//传递每列给GetSqlParameterStatement函数

<%= GetSqlParameterStatement(SourceTable.Columns[i]) %>

//循环结束条件

<% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>

<% } % >

AS

定义GetSqlParameterStatement方法接受列参数返回参数名,和字段类型,注意函数要放在

<script runat="template"></script>里

<script runat="template">

public string GetSqlParameterStatement(ColumnSchema column)

{

string param = "@" + column.Name + " " + column.NativeType;

switch (column.DataType)

{

case DbType.Decimal:

{

param += "(" + column.Precision + ", " + column.Scale + ")";

break;

}

default:

{

if (column.Size > 0)

{

param += "(" + column.Size + ")";

}

break;

}

}

return param;

}

</script>

接下来创建存储过程的Update部分,语法类似不再说明

UPDATE [<%= SourceTable.Name %>] SET

<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>

[<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>

<% } %>

//where条件,注意主键提取方法

WHERE

<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>

<% if (i > 0) { %>AND <% } %>

[<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>

<% } %>

最终为模板为如下形式

<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"

Description="Generates a update stored procedure." %>

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"

Category="Context"

Description="Table that the stored procedures should be based on." %>

<%@ Assembly Name="SchemaExplorer" %>

<%@ Import Namespace="SchemaExplorer" %>

<script runat="template">

public string GetSqlParameterStatement(ColumnSchema column)

{

string param = "@" + column.Name + " " + column.NativeType;

switch (column.DataType)

{

case DbType.Decimal:

{

param += "(" + column.Precision + ", " + column.Scale + ")";

break;

}

default:

{

if (column.Size > 0)

{

param += "(" + column.Size + ")";

}

break;

}

}

return param;

}

</script>

CREATE PROCEDURE dbo.Update<%= SourceTable.Name %>

<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>

<%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>

<% } %>

AS

UPDATE [<%= SourceTable.Name %>] SET

<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>

[<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>

<% } %>

WHERE

<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>

<% if (i > 0) { %>AND <% } %>

[<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>

<% } %>

将其用CodeSmith Explorer.exe打开

可以任意选择数据库中的表,这里选择NorthWind中的0rders表

然后生成代码,就创建好了存储过程

CREATE PROCEDURE dbo.UpdateOrders

@OrderID int,

@CustomerID nchar(5),

@EmployeeID int,

@OrderDate datetime,

@RequiredDate datetime,

@ShippedDate datetime,

@ShipVia int,

@Freight money,

@ShipName nvarchar(40),

@ShipAddress nvarchar(60),

@ShipCity nvarchar(15),

@ShipRegion nvarchar(15),

@ShipPostalCode nvarchar(10),

@ShipCountry nvarchar(15)

AS

UPDATE [Orders] SET

[CustomerID] = @CustomerID,

[EmployeeID] = @EmployeeID,

[OrderDate] = @OrderDate,

[RequiredDate] = @RequiredDate,

[ShippedDate] = @ShippedDate,

[ShipVia] = @ShipVia,

[Freight] = @Freight,

[ShipName] = @ShipName,

[ShipAddress] = @ShipAddress,

[ShipCity] = @ShipCity,

[ShipRegion] = @ShipRegion,

[ShipPostalCode] = @ShipPostalCode,

[ShipCountry] = @ShipCountry

WHERE

[OrderID] = @OrderID

同样,我们可以选择其他的表创建Updata存储过程,也可以自定义其他诸如insert delete等的存储过程,是不是很方便?^-^

以上只是CodeSmith的简单应用,还有其他的功能有待大家探索了。

关于CodeSmith社区一些模板资源,大家可以去如下地址选择下载

CodeSmith Peer Support Forum

Codesmith templates library

<script runat="template">

public string GetSqlParameterStatement(ColumnSchema column)

{

string param = "@" + column.Name + " " + column.NativeType;

switch (column.DataType)

{

case DbType.Decimal:

{

param += "(" + column.Precision + ", " + column.Scale + ")";

break;

}

default:

{

if (column.Size > 0)

{

param += "(" + column.Size + ")";

}

break;

}

}

return param;

}

</script>

接下来创建存储过程的Update部分,语法类似不再说明

UPDATE [<%= SourceTable.Name %>] SET

<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>

[<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>

<% } %>

//where条件,注意主键提取方法

WHERE

<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>

<% if (i > 0) { %>AND <% } %>

[<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>

<% } %>

最终为模板为如下形式

<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"

Description="Generates a update stored procedure." %>

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"

Category="Context"

Description="Table that the stored procedures should be based on." %>

<%@ Assembly Name="SchemaExplorer" %>

<%@ Import Namespace="SchemaExplorer" %>

<script runat="template">

public string GetSqlParameterStatement(ColumnSchema column)

{

string param = "@" + column.Name + " " + column.NativeType;

switch (column.DataType)

{

case DbType.Decimal:

{

param += "(" + column.Precision + ", " + column.Scale + ")";

break;

}

default:

{

if (column.Size > 0)

{

param += "(" + column.Size + ")";

}

break;

}

}

return param;

}

</script>

CREATE PROCEDURE dbo.Update<%= SourceTable.Name %>

<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>

<%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>

<% } %>

AS

UPDATE [<%= SourceTable.Name %>] SET

<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>

[<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>

<% } %>

WHERE

<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>

<% if (i > 0) { %>AND <% } %>

[<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>

<% } %>

将其用CodeSmith Explorer.exe打开

可以任意选择数据库中的表,这里选择NorthWind中的0rders表

然后生成代码,就创建好了存储过程

CREATE PROCEDURE dbo.UpdateOrders

@OrderID int,

@CustomerID nchar(5),

@EmployeeID int,

@OrderDate datetime,

@RequiredDate datetime,

@ShippedDate datetime,

@ShipVia int,

@Freight money,

@ShipName nvarchar(40),

@ShipAddress nvarchar(60),

@ShipCity nvarchar(15),

@ShipRegion nvarchar(15),

@ShipPostalCode nvarchar(10),

@ShipCountry nvarchar(15)

AS

UPDATE [Orders] SET

[CustomerID] = @CustomerID,

[EmployeeID] = @EmployeeID,

[OrderDate] = @OrderDate,

[RequiredDate] = @RequiredDate,

[ShippedDate] = @ShippedDate,

[ShipVia] = @ShipVia,

[Freight] = @Freight,

[ShipName] = @ShipName,

[ShipAddress] = @ShipAddress,

[ShipCity] = @ShipCity,

[ShipRegion] = @ShipRegion,

[ShipPostalCode] = @ShipPostalCode,

[ShipCountry] = @ShipCountry

WHERE

[OrderID] = @OrderID

同样,我们可以选择其他的表创建Updata存储过程,也可以自定义其他诸如insert delete等的存储过程,是不是很方便?^-^

以上只是CodeSmith的简单应用,还有其他的功能有待大家探索了。

关于CodeSmith社区一些模板资源,大家可以去如下地址选择下载

CodeSmith Peer Support Forum

Codesmith templates library

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a simple class " %>

然后声明几个变量,用来为了能够在以后生成的代码嵌入,这里声明了三个名为NameSpace,ClassName,Contxt的变量。其他参数一目了然就不再说明了,

<%@ Property Name="NameSpace" Type="String"

Category="Context"

Description="The namespace to use for this class" %>

<%@ Property Name="ClassName" Type="String"

Category="Context"

Description="The name of the class to generate" %>

<%@ Property Name="DevelopersName" Type="String"

Category="Context"

Description="The name to include in the comment header" %>

接下来建立将要生成代码的框架,在适当位置引用刚刚声明的变量名

using System;

namespace <%=NameSpace %>

{

/// <summary>

/// Summary description for <%=ClassName %>.

/// </summary>

public class <%=ClassName %>

{

public <%=ClassName %>()

{

}

}

}

最后,打开CodeSmith Explorer.exe,加载此模板,并且在属性对话框中任意更改声明的变量名,按Generate按钮生成合适的代码

以上简单的说明了CodeSmith的功能

下面看看在数据库访问中它是如何最小化我们的工作的

在这里我们要用到CodeSmith API中一个叫SchemaExplorer的组件,它提供了一系列类来操作数据库的框架,我们可以用它来创建表和存储过程,得到字段类型,字段名等。

如下是更新NorthWind数据库中orders表记录的存储过程,我们来看看如何自动生成它

CREATE PROCEDURE dbo.UpdateOrders

@OrderID int,

@CustomerID nchar(5),

@EmployeeID int,

@OrderDate datetime,

@RequiredDate datetime,

@ShippedDate datetime,

@ShipVia int,

@Freight money,

@ShipName nvarchar(40),

@ShipAddress nvarchar(60),

@ShipCity nvarchar(15),

@ShipRegion nvarchar(15),

@ShipPostalCode nvarchar(10),

@ShipCountry nvarchar(15)

AS

UPDATE [Orders] SET

[CustomerID] = @CustomerID,

[EmployeeID] = @EmployeeID,

[OrderDate] = @OrderDate,

[RequiredDate] = @RequiredDate,

[ShippedDate] = @ShippedDate,

[ShipVia] = @ShipVia,

[Freight] = @Freight,

[ShipName] = @ShipName,

[ShipAddress] = @ShipAddress,

[ShipCity] = @ShipCity,

[ShipRegion] = @ShipRegion,

[ShipPostalCode] = @ShipPostalCode,

[ShipCountry] = @ShipCountry

WHERE

[OrderID] = @OrderID

第一步还是创建一个CodeTemplate指示,注意TargetLanguage属性改为了T-SQL,因为创建的SQL语言代码

<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Description="Generates a update stored procedure." %>

然后加载SchemaExplorer组件,并导入SchemaExplorer命名空间,看这里是不是和asp.net很像

<%@ Assembly Name="SchemaExplorer" %>

<%@ Import Namespace="SchemaExplorer" %>

接下来声明变量,因为要从数据库表中读取框架所以 Type 属性为SchemaExplorer.TableSchema

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the stored procedures should be based on." %>

下面就要写实际将要输出代码部分的模板了。

先写存储过程的第一行 ,<%%>内为引用前面声明变量名,代表表名

CREATE PROCEDURE dbo.Update<%=SourceTable.Name %>

第二步为存储过程创造将要声明的参数列表

//开始循环遍历每列

<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>

//传递每列给GetSqlParameterStatement函数

<%= GetSqlParameterStatement(SourceTable.Columns[i]) %>

//循环结束条件

<% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>

<% } % >

AS

定义GetSqlParameterStatement方法接受列参数返回参数名,和字段类型,注意函数要放在

<script runat="template"></script>里

<script runat="template">

public string GetSqlParameterStatement(ColumnSchema column)

{

string param = "@" + column.Name + " " + column.NativeType;

switch (column.DataType)

{

case DbType.Decimal:

{

param += "(" + column.Precision + ", " + column.Scale + ")";

break;

}

default:

{

if (column.Size > 0)

{

param += "(" + column.Size + ")";

}

break;

}

}

return param;

}

</script>

接下来创建存储过程的Update部分,语法类似不再说明

UPDATE [<%= SourceTable.Name %>] SET

<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>

[<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>

<% } %>

//where条件,注意主键提取方法

WHERE

<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>

<% if (i > 0) { %>AND <% } %>

[<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>

<% } %>

最终为模板为如下形式

<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"

Description="Generates a update stored procedure." %>

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"

Category="Context"

Description="Table that the stored procedures should be based on." %>

<%@ Assembly Name="SchemaExplorer" %>

<%@ Import Namespace="SchemaExplorer" %>

<script runat="template">

public string GetSqlParameterStatement(ColumnSchema column)

{

string param = "@" + column.Name + " " + column.NativeType;

switch (column.DataType)

{

case DbType.Decimal:

{

param += "(" + column.Precision + ", " + column.Scale + ")";

break;

}

default:

{

if (column.Size > 0)

{

param += "(" + column.Size + ")";

}

break;

}

}

return param;

}

</script>

CREATE PROCEDURE dbo.Update<%= SourceTable.Name %>

<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>

<%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>

<% } %>

AS

UPDATE [<%= SourceTable.Name %>] SET

<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>

[<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>

<% } %>

WHERE

<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>

<% if (i > 0) { %>AND <% } %>

[<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>

<% } %>

将其用CodeSmith Explorer.exe打开

可以任意选择数据库中的表,这里选择NorthWind中的0rders表

然后生成代码,就创建好了存储过程

CREATE PROCEDURE dbo.UpdateOrders

@OrderID int,

@CustomerID nchar(5),

@EmployeeID int,

@OrderDate datetime,

@RequiredDate datetime,

@ShippedDate datetime,

@ShipVia int,

@Freight money,

@ShipName nvarchar(40),

@ShipAddress nvarchar(60),

@ShipCity nvarchar(15),

@ShipRegion nvarchar(15),

@ShipPostalCode nvarchar(10),

@ShipCountry nvarchar(15)

AS

UPDATE [Orders] SET

[CustomerID] = @CustomerID,

[EmployeeID] = @EmployeeID,

[OrderDate] = @OrderDate,

[RequiredDate] = @RequiredDate,

[ShippedDate] = @ShippedDate,

[ShipVia] = @ShipVia,

[Freight] = @Freight,

[ShipName] = @ShipName,

[ShipAddress] = @ShipAddress,

[ShipCity] = @ShipCity,

[ShipRegion] = @ShipRegion,

[ShipPostalCode] = @ShipPostalCode,

[ShipCountry] = @ShipCountry

WHERE

[OrderID] = @OrderID

同样,我们可以选择其他的表创建Updata存储过程,也可以自定义其他诸如insert delete等的存储过程,是不是很方便?^-^

以上只是CodeSmith的简单应用,还有其他的功能有待大家探索了。

关于CodeSmith社区一些模板资源,大家可以去如下地址选择下载

CodeSmith Peer Support Forum

Codesmith templates library

<script runat="template">

public string GetSqlParameterStatement(ColumnSchema column)

{

string param = "@" + column.Name + " " + column.NativeType;

switch (column.DataType)

{

case DbType.Decimal:

{

param += "(" + column.Precision + ", " + column.Scale + ")";

break;

}

default:

{

if (column.Size > 0)

{

param += "(" + column.Size + ")";

}

break;

}

}

return param;

}

</script>

接下来创建存储过程的Update部分,语法类似不再说明

UPDATE [<%= SourceTable.Name %>] SET

<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>

[<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>

<% } %>

//where条件,注意主键提取方法

WHERE

<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>

<% if (i > 0) { %>AND <% } %>

[<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>

<% } %>

最终为模板为如下形式

<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"

Description="Generates a update stored procedure." %>

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"

Category="Context"

Description="Table that the stored procedures should be based on." %>

<%@ Assembly Name="SchemaExplorer" %>

<%@ Import Namespace="SchemaExplorer" %>

<script runat="template">

public string GetSqlParameterStatement(ColumnSchema column)

{

string param = "@" + column.Name + " " + column.NativeType;

switch (column.DataType)

{

case DbType.Decimal:

{

param += "(" + column.Precision + ", " + column.Scale + ")";

break;

}

default:

{

if (column.Size > 0)

{

param += "(" + column.Size + ")";

}

break;

}

}

return param;

}

</script>

CREATE PROCEDURE dbo.Update<%= SourceTable.Name %>

<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>

<%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>

<% } %>

AS

UPDATE [<%= SourceTable.Name %>] SET

<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>

[<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>

<% } %>

WHERE

<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>

<% if (i > 0) { %>AND <% } %>

[<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>

<% } %>

将其用CodeSmith Explorer.exe打开

可以任意选择数据库中的表,这里选择NorthWind中的0rders表

然后生成代码,就创建好了存储过程

CREATE PROCEDURE dbo.UpdateOrders

@OrderID int,

@CustomerID nchar(5),

@EmployeeID int,

@OrderDate datetime,

@RequiredDate datetime,

@ShippedDate datetime,

@ShipVia int,

@Freight money,

@ShipName nvarchar(40),

@ShipAddress nvarchar(60),

@ShipCity nvarchar(15),

@ShipRegion nvarchar(15),

@ShipPostalCode nvarchar(10),

@ShipCountry nvarchar(15)

AS

UPDATE [Orders] SET

[CustomerID] = @CustomerID,

[EmployeeID] = @EmployeeID,

[OrderDate] = @OrderDate,

[RequiredDate] = @RequiredDate,

[ShippedDate] = @ShippedDate,

[ShipVia] = @ShipVia,

[Freight] = @Freight,

[ShipName] = @ShipName,

[ShipAddress] = @ShipAddress,

[ShipCity] = @ShipCity,

[ShipRegion] = @ShipRegion,

[ShipPostalCode] = @ShipPostalCode,

[ShipCountry] = @ShipCountry

WHERE

[OrderID] = @OrderID

同样,我们可以选择其他的表创建Updata存储过程,也可以自定义其他诸如insert delete等的存储过程,是不是很方便?^-^

以上只是CodeSmith的简单应用,还有其他的功能有待大家探索了。

关于CodeSmith社区一些模板资源,大家可以去如下地址选择下载

CodeSmith Peer Support Forum

Codesmith templates library

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有