简体   繁体   中英

Use of the T4 Template Type in Visual Studio

I Would like to know whether i can read data from a database and generate a configuration file using the T4 templating feature of Visual Studio 2010.

Basically, i deploy my project as a visual studio add-in. So, when a user creates a new application from my template, then i need to get some data from the db and then generate a config file that the user can use in their application.

Is this possible in T4 [.tt] or is there any other way to accomplish this functionality in visual studio 2010. If so give me link so that i can get a hands-on and get back in case of clarifications.

Yes, of course you can. You can write any c# code in t4, so if you can write it in c#, you can write it in tt.

For example, this TT file would output the content of some table to desired file:

    <#@ template language="C#v3.5" hostspecific="true"#>
    <#@ output extension="cs" #>
    <#@ assembly name="System.Core" #>
    <#@ assembly name="System.Data" #>
    <#@ assembly name="System.Data.DataSetExtensions" #>
    <#@ assembly name="System.Xml" #>
    <#@ assembly name="System.Xml.Linq" #>
    <#@ import namespace="System.IO" #>
    <#@ import namespace="System.Data.SqlClient" #>
    <#@ import namespace="System.Data" #>
    <#@ import namespace="System.Collections.Generic" #>
    <#@ import namespace="System.Xml" #>
    <#@ import namespace="System.Xml.Linq" #>
    <#@ import namespace="System.Text" #>
    <#@ import namespace="System.Linq" #>
    <#
    string outputFileName = @"tableContent.txt";

    string dbConnection = @"Data Source=local;Initial Catalog=mydb;user=sa;password=sa";


    System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(dbConnection);

    #>

    //Some info like 'this is generated code, do not modify' or similar... 
    //this text (because it's out of the tt region) goes into the output file

    <#
    //again, the c# code...
            SqlConnection conn = new SqlConnection(dbConnection);
            conn.Open();
            PushIndent("\t");
            string query = "SELECT something FROM myTable";
            using (IDataReader reader = new SqlCommand(query, conn).ExecuteReader())
            {
                Write("Undefined = 0");
                while(reader.Read())
                { 
                    if (reader[0] != DBNull.Value) 
                    {
                        Write("Value=" + reader[0].ToString() + Environment.NewLine);
                    }
                }
                WriteLine("");
            }
            PopIndent();
            conn.Close();

    #>
    //this goes into file end (could've also be there with Write function)

    <#
    //output to the desired file
    if (!String.IsNullOrEmpty(outputFileName))
    {
        File.WriteAllText(outputFileName, this.GenerationEnvironment.ToString());
        this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
    }

    #>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM