zoukankan      html  css  js  c++  java
  • CodeSmith实用技巧(十二):自动执行SQL脚本

    CodeSmith中,如果生成的代码是SQL脚本,则可以在生成代码完成时自动执行生成的代码,也就是在生成的SQL脚本的同时在数据库中创建新的对象。

    BaseTemplates.ScriptUtility对象提供ExecuteScript方法可以实现,如果想在生成代码完成后立即执行生成的脚本,可以很方便的通过重载OnPostRender来实现。

    在使用之前,先添加对下列程序集的引用:

    <%@ Assembly Name="CodeSmith.BaseTemplates" %>
    <%@ Import Namespace="CodeSmith.BaseTemplates" %>

    看下面的这个例子:

    protected override void OnPostRender(string result) 
    {
        
    // execute the output on the same database as the source table.
        CodeSmith.BaseTemplates.ScriptResult scriptResult = 
         CodeSmith.BaseTemplates.ScriptUtility.ExecuteScript(
    this.SourceTable.Database.ConnectionString, 
         result, 
    new System.Data.SqlClient.SqlInfoMessageEventHandler(cn_InfoMessage)); 
        Trace.Write(scriptResult.ToString());
        
    base.OnPostRender(result);
    }


    在这个例子中SourceTable是一个类型为SchemaExplorer.TableSchema.的属性,使用的时候需要调整部分代码来获取数据库的连接以便在生成代码完成后执行脚本。

    支持TerryLee的创业产品Worktile
    Worktile,新一代简单好用、体验极致的团队协同、项目管理工具,让你和你的团队随时随地一起工作。完全免费,现在就去了解一下吧。
    https://worktile.com
  • 相关阅读:
    Linux下的ping命令
    git stash
    ansiable
    「疫期集训day4」硝烟
    「线段树」「单点修改」洛谷P1198 [JSOI2008]最大数
    「状压DP」「暴力搜索」排列perm
    「疫期集训day3」要塞
    「疫期集训day2」高地
    「疫期集训day1」无言
    「区间DP」「洛谷P1043」数字游戏
  • 原文地址:https://www.cnblogs.com/Terrylee/p/306800.html
Copyright © 2011-2022 走看看