CodeSmith 使用



 CodeSmith 以前只是听说,用了一下还不错。适合我这种每天写垃圾代码的人

<% @ 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. "   %>
<% @ Assembly Name = " SchemaExplorer "   %>
<% @ Import Namespace = " SchemaExplorer "   %>
<% Property  Name = " SourceTable "  Type = " SchemaExplorer.TableSchema "  Category = " DataTable "  Description = " Table that the stored procedures should be based on. "   %>
<% Property  Name = " Author "  Type = " String "  Category = " Context "  Description = " The author for this procedure. "  Optional = " true " %>
<% Property  Name = " Description "  Type = " String "  Category = " Context "  Description = " The description for this procedure. "  Optional = " true " %>
< 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;
}
public string GetUpdateParameter()
{   string temp
= null ;
    
for  ( int  i = 0 ;i < SourceTable.Columns.Count;i ++ )
    { 
    
if  ( ! SourceTable.Columns[i].IsPrimaryKeyMember)
        { 
            temp
= temp + SourceTable.Columns[i].Name + " =@ " + SourceTable.Columns[i].Name + " , " + " \n " ;
         }
    }
    
if  (temp.Length  > 0 )
     
return  temp.Substring ( 0 ,temp.Length - 1 );
    
else
    
return   "" ;
    
}
</ script >
CREATE PROCEDURE dbo.
<% = SourceTable.Name  %> Delete
/*
==================================================
??:
<% =  Author  %>
??????:
<% =  System.DateTime.Now.ToShortDateString()  %>
??:
<% =  Description  %>
==================================================
*/
<%   for  ( int  i  =   0 ; i  <  SourceTable.PrimaryKey.MemberColumns.Count; i ++ ) {  %>
<% =  GetSqlParameterStatement(SourceTable.PrimaryKey.MemberColumns[i])  %><%   if  (i  <  SourceTable.PrimaryKey.MemberColumns.Count  -   1 ) {  %> , <%  }  %>      <%   if  (SourceTable.Columns[i].Description ! =   "" ) {  %> -- <% =  SourceTable.Columns[i].Description  %><%  }  %>
<%  }  %>
AS
Delete From [
<% =  SourceTable.Name  %>
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  %>
<%  }  %>



CREATE PROCEDURE dbo.
<% = SourceTable.Name  %> SelectByID
/*
==================================================
Author:
<% =  Author  %>
CreatedTime:
<% =  System.DateTime.Now.ToShortDateString()  %>
Description:
<% =  Description  %>
==================================================
*/
<%   for  ( int  i  =   0 ; i  <  SourceTable.PrimaryKey.MemberColumns.Count; i ++ ) {  %>
<% =  GetSqlParameterStatement(SourceTable.PrimaryKey.MemberColumns[i])  %><%   if  (i  <  SourceTable.PrimaryKey.MemberColumns.Count  -   1 ) {  %> , <%  }  %>      <%   if  (SourceTable.Columns[i].Description ! =   "" ) {  %> -- <% =  SourceTable.Columns[i].Description  %><%  }  %>
<%  }  %>

AS
select 
<%   for  ( int  i  =   0 ; i  <  SourceTable.Columns.Count; i ++ ) {  %>
<% =  SourceTable.Columns[i].Name  %>
<%  }  %>

From [
<% =  SourceTable.Name  %>
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  %>
<%  }  %>


CREATE PROCEDURE dbo.
<% = SourceTable.Name  %> SelectAll
/*
==================================================
Author:
<% =  Author  %>
CreatedTime:
<% =  System.DateTime.Now.ToShortDateString()  %>
Description:
<% =  Description  %>
==================================================
*/

AS
select 
<%   for  ( int  i  =   0 ; i  <  SourceTable.Columns.Count; i ++ ) {  %>
<% =  SourceTable.Columns[i].Name  %>
<%  }  %>

From [
<% =  SourceTable.Name  %>


CREATE PROCEDURE dbo.
<% = SourceTable.Name  %> Update
/*
==================================================
Author:
<% =  Author  %>
CreatedTime:
<% =  System.DateTime.Now.ToShortDateString()  %>
Description:
<% =  Description  %>
==================================================
*/
<%   for  ( int  i  =   0 ; i  <  SourceTable.Columns.Count; i ++ ) {  %>
<% =  GetSqlParameterStatement(SourceTable.Columns[i])  %>
<%  }  %>
AS
Update  [
<% =  SourceTable.Name  %> ]  set 

<% = GetUpdateParameter() %>
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  %>
<%  }  %>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值