• CodeSmith 5.0工具实例篇系列4——根据表生成修改的存储过程,针对MS Sqlserver


    运行该模板时,只需要选择单个表即可。
    申明:该系列案例已通过CodeSmith Professional 5.0.1 Revision 4983版本的测试,以及生成的存储过程是针对MS Sqlserver。

    操作说明 :运行CodeSmith Studio工具 ,创建Blank Template,并将文件名命名为:Update Proc.cst。
    输入以下代码:


    <%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Src="" Inherits="" Debug="False" LinePragmas="True" Description="根据表生成修改的存储过程"  ResponseEncoding="UTF-8"%>
    <%@ Property Name="TableName" Type="SchemaExplorer.TableSchema" Default="" Optional="False" Category="" Description="请选择需要生成存储过程的表" OnChanged="" Editor="" EditorBase="" Serializer="" %>
    <%@ Assembly Name="SchemaExplorer" %>
    <%@ Import Namespace="SchemaExplorer" %>

    ------------------------------------
    --说明:<%=TableName%>表的修改存储过程 
    --时间:<%= DateTime.Now %>
    --技术支持:时间太快了
    --邮箱地址:amwggyy504@163.com
    --文章来源:http://amwggyy504.cnblogs.com
    ------------------------------------
    <%
    string StrWhere="";//修改条件 
    string SetValues="";//修改的值 。
    string Param="";//传入的参数。
    foreach(ColumnSchema Column in TableName.Columns)
    {           
            SetValues
    +="["+Column.Name+"]"+"=@"+Column.Name;
        Param
    +="@"+Column.Name+" " + Column.NativeType;
        
    switch(Column.DataType)
        {
            
    case DbType.Decimal:
            {
                Param
    +="("+Column.Precision+","+Column.Scale+")";
                
    break;
            }
            
    case DbType.Int32:
            {                                
                
    break;
            }
                    
    case DbType.DateTime:
                    {
                            
    break;
                            }
            
    default:
            {
                
    if(Column.Size>0)
                        Param
    +="("+Column.Size+")";                                        
                
    break;
            }                        
        }        
        
    if(Column.Name!=TableName.Columns[TableName.Columns.Count-1].Name)
        {        
            SetValues
    +=",\n";
            Param
    +=",\n";
        }
    }

    for (int i=0;i<TableName.PrimaryKey.MemberColumns.Count; i++)
    {
            
    if (i>0) { 
                    StrWhere
    +=" And " ;
                    }
                    StrWhere
    +="["+TableName.PrimaryKey.MemberColumns[i].Name +"]=@"+ TableName.PrimaryKey.MemberColumns[i].Name;
    }
    %>
    Create Proc Proc_
    <%=TableName.Name%>_Update
            
    <%=Param%>
    As
            Update  
    <%=TableName.Name%>
                   Set 
    <%=SetValues %>
                    Where   
    <%=StrWhere %>
  • 相关阅读:
    结语
    Print all nodes at distance k from a given node
    Construct a tree from Inorder and Level order traversals
    Transform a BST to greater sum tree
    Minimum no. of iterations to pass information to all nodes in the tree
    Print a Binary Tree in Vertical Order
    Rearrange a string so that all same characters become d distance away
    Suffix array
    cocos2d-x 3.2 椭圆运动
    cocos2d-x 3.2 DrawNode 绘图API
  • 原文地址:https://www.cnblogs.com/juan/p/1424367.html
Copyright © 2020-2023  润新知