CodeSmith使用第二回(再看你一回)存储过程篇

今天看了看几个例子,练习了如何生成数据库存储过程,感觉很爽啊!哈哈
 1  <% @ CodeTemplate Language = " C# "  TargetLanguage = " T-SQL "  Src = ""  Inherits = ""  Debug = " False "  Description = " Template description here. "   %>
 2  <% @ Property Name = " SourceTable "  Type = " SchemaExplorer.TableSchema "  Description = " ????-??. "   %>
 3  <% @ Assembly Name = " SchemaExplorer "   %>
 4  <% @ Import Namespace = " SchemaExplorer "   %>
 5  < script runat = " template " >
 6  public   string  getParameter(ColumnSchema col)
 7  {
 8  String parameter = " @ " + col.Name + "   " + col.NativeType;
 9  switch (col.DataType)
10  {
11  case  DbType.Decimal:
12  {
13  parameter += " ( " + col.Precision + " , " + col.Scale + " ) " ;
14  break ;
15  }
16  default :
17  {
18  if (col.Size > 0 )
19  {
20  parameter += " ( " + col.Size + " ) " ;
21  }
22  break ;
23  }
24  }
25  return  parameter;
26  }
27  </ script >
28  create procedure dbo.Delete <%= SourceTable.Name %>
29  <% for ( int  i = 0 ;i < SourceTable.PrimaryKey.MemberColumns.Count;i ++ ){ %>
30  <%= getParameter(SourceTable.PrimaryKey.MemberColumns[i]) %>
31  <%   if (i > 0 ){ %> , <% } %>
32  <% } %>
33  AS
34  delete  *  from  <%= SourceTable.Name %>
35  where
36  <% for  ( int  i = 0 ;i < SourceTable.PrimaryKey.MemberColumns.Count;i ++ ){ %>
37  <% if ( i > 0 ){ %> and <% } %>
38  <%= SourceTable.PrimaryKey.MemberColumns[i].Name %>= @ <%= SourceTable.PrimaryKey.MemberColumns[i].Name %>
39  <% } %>
首先这是一个把模板编译成代码的IDE,可以用C#和vb.net编写代码,点击为选择的数据库表生成删除的存储过程,很爽哦,再来个更新的,^_^
update
 1 <%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"  Description="????" %>
 2 <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="??????????" %>
 3 <%@ Assembly Name="SchemaExplorer" %>
 4 <%@ Import Namespace="SchemaExplorer" %>
 5 
 6 CREATE PROCEDURE dbo.update<%=SourceTable.Name%>
 7 <% for(int i=0;i<SourceTable.Columns.Count;i++){%>
 8 <%=getParameter(SourceTable.Columns[i])%>
 9 <% if(i<SourceTable.Columns.Count-1){%>,<% }%>
10 <% }%>
11 AS
12 Update <%=SourceTable.Name%> set
13 <% for(int i=0;i<SourceTable.NonPrimaryKeyColumns.Count;i++ ){%>
14 <%=SourceTable.NonPrimaryKeyColumns[i].Name%>=@<%=SourceTable.NonPrimaryKeyColumns[i].Name%>
15 <%if(SourceTable.NonPrimaryKeyColumns.Count-1>1){%>,<%}%>
16 <%}%>
17 where
18 <% for(int i=0;i<SourceTable.PrimaryKey.MemberColumns.Count;i++){%>
19 <%if(i>0){%>and<%}%>
20 <%=SourceTable.PrimaryKey.MemberColumns[i].Name%>=@<%=SourceTable.PrimaryKey.MemberColumns[i].Name%>
21 <%}%>
22 <script runat="template">
23 public string getParameter(ColumnSchema col)
24 {
25 String parameter="@"+col.Name+" "+col.NativeType;
26 switch(col.DataType)
27 {
28 case DbType.Decimal:
29 {
30 parameter+="("+col.Precision+","+col.Scale+")";
31 break;
32 }
33 default:
34 {
35 if(col.Size>0)
36 {
37 parameter+="("+col.Size+")";
38 }
39 break;
40 }
41 }
42 return parameter;
43 }
44 </script>
菜鸟火速进步中,前人铺好路,学起来也比较轻松,偶尔要加上自己的一点小创意啊,哈哈!

转载于:https://www.cnblogs.com/Ablog-sunny/archive/2007/08/28/873486.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值