T4 模板从数据库模式为每个表创建*多个* html(例如)输出文件 [英] T4 template to create *multiple* html (for example) output files per table from database schema

查看:15
本文介绍了T4 模板从数据库模式为每个表创建*多个* html(例如)输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一些 T4 模板来生成派生自 sql server(在本例中)数据库架构的 html 文件.对于数据库中的每个表,我想创建 2 个文件:

I'd like to use some T4 templates to generate html files derived from a sql server (in this case) database schema. For each table in the database, I would like to create 2 files:

  1. tableName_List.aspx - 将包含适当的 html 以在 asp.net GridView 中显示,并为每个 db 表列定义一个网格列

tableName_Edit.aspx - 将包含适当的 html 以在 asp.net FormView 中显示,并带有一个文本框(为简单起见,现在)用于每个 db 表列

tableName_Edit.aspx - would contain the appropriate html to display in an asp.net FormView, with a textbox (for simplicity's sake, for now) for each db table column

所以,如果我在数据库中有 5 个表,我会得到 10 个文件输出.我一直在谷歌搜索并找到相关文章,但其中大多数似乎没有解决这种情况.我也看到过为此使用亚音速的参考资料,但我不想再将另一种技术引入其中.

So, if I have 5 tables in the database, I would get 10 files output. I've been googling this and found related articles, but most of them don't seem to address this scenario. I've also seen references to using subsonic for this, but I'd rather not introduce yet another technology into the mix.

推荐答案

下面的 T4 模板代码会给你一个比较好的开始.

The code for the T4 template below will give you a relatively good start.

您需要向项目中添加对适当版本的 Microsoft.SqlSserver Smo DLL 的引用.

You will need to add references to the appropriate versions of the Microsoft.SqlSserver Smo DLLs to the project.

此代码中的以下项目需要替换为适合您环境的值:

The following items need to be replaced in this code with the appropriate values for your environment:

SERVERNAMEGOESHERE
数据库名称GOESHERE
项目名称空间去这里

SERVERNAMEGOESHERE
DATABASENAMEGOESHERE
PROJECTNAMESPACEGOESHERE

<#@ template language="C#v3.5" hostspecific="true" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
<#@ assembly name="Microsoft.SqlServer.Smo" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="Microsoft.SqlServer.Management.Common" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
<#
    string connectionString = @"Server=SERVERNAMEGOESHERE;Trusted_Connection=True;";
    string databaseName = "DATABASENAMEGOESHERE";
    string projectNamespace = "PROJECTNAMESPACEGOESHERE";
    string relativeOutputFilePath = null;

    SqlConnection oneSqlConnection = new SqlConnection(connectionString);
    ServerConnection oneServerConnection = new ServerConnection(oneSqlConnection);
    Server oneServer = new Server(oneServerConnection);
    Database oneDatabase = oneServer.Databases[databaseName];
    foreach (Table oneTable in oneDatabase.Tables)
    {
        if (!oneTable.Name.Equals("sysdiagrams"))
        {
#>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="<#= oneTable.Name #>_List.aspx.cs" Inherits="<#= projectNamespace #>.<#= oneTable.Name #>_List" %>
<asp:DataGrid ID="<#= oneTable.Name #>DataGrid" runat="server" AutoGenerateColumns="false">
    <Columns>
<#
            foreach (Column oneColumn in oneTable.Columns)
            {
#>
        <asp:BoundColumn DataField="<#= oneColumn.Name #>" HeaderText="<#= oneColumn.Name #>"></asp:BoundColumn>
<#
            }
#>
    </Columns>
</asp:DataGrid>
<#
            relativeOutputFilePath = @"\Output\" + oneTable.Name + "_List.aspx";
            TemplateHelper.WriteTemplateOutputToFile(relativeOutputFilePath, Host, GenerationEnvironment);
            GenerationEnvironment = new System.Text.StringBuilder();
#>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="<#= oneTable.Name #>_Edit.aspx.cs" Inherits="<#= projectNamespace #>.<#= oneTable.Name #>_Edit" %>
<#
            foreach (Column oneColumn in oneTable.Columns)
            {
#>
    <asp:TextBox ID="<#= oneColumn.Name #>TextBox" runat="server" />
<#
            }
            relativeOutputFilePath = @"\Output\" + oneTable.Name + "_Edit.aspx";
            TemplateHelper.WriteTemplateOutputToFile(relativeOutputFilePath, Host, GenerationEnvironment);
            GenerationEnvironment = new System.Text.StringBuilder();
        }
    }
#>
<#+
public class TemplateHelper
{
    public static void WriteTemplateOutputToFile(
        string relativeOutputFilePath,
        Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost Host,
        System.Text.StringBuilder GenerationEnvironment)
    {
        string outputPath = System.IO.Path.GetDirectoryName(Host.TemplateFile);
        string outputFilePath = outputPath + relativeOutputFilePath;
        System.IO.File.WriteAllText(outputFilePath, GenerationEnvironment.ToString());
    }
}
#>

这篇关于T4 模板从数据库模式为每个表创建*多个* html(例如)输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆