简体   繁体   English

T4代问题

[英]T4 generation issues

I'm trying to learn some C# and coming from a data background.我正在尝试学习一些 C# 并来自数据背景。 Now I want to generate some SQL code based on a database.现在我想基于数据库生成一些 SQL 代码。 I'm working with the examples from https://damieng.com/blog/2009/11/06/multiple-outputs-from-t4-made-easy-revisited .我正在使用https://damieng.com/blog/2009/11/06/multiple-outputs-from-t4-made-easy-revisited中的示例。 When I download the solution from Github ( https://github.com/DanielGasson/t4-examples ) it works.当我从 Github ( https://github.com/DanielGasson/t4-examples )下载解决方案时,它可以工作。

But I'm having some issues with this.但我对此有一些问题。 I hope that somebody can help me a little because that I've tried many things and I get frustrated that this doesn't work.... So maybe somebody can get me back on track with some hints:我希望有人可以帮助我,因为我已经尝试了很多事情,但我对这不起作用感到沮丧......所以也许有人可以通过一些提示让我回到正轨:

My issues:我的问题:

Problem 1 - adding SQL connection I want to add some SQL to retieve some data that must be generated but now I have a C# project which gives problems when I try to add some SQL connection code like: Problem 1 - adding SQL connection I want to add some SQL to retieve some data that must be generated but now I have a C# project which gives problems when I try to add some SQL connection code like:

SqlConnection conn = new SqlConnection(connectionString); 
string selectQueryTables = "select * from [config].[TargetTables]"; 
SqlDataAdapter commandTables = new SqlDataAdapter(selectQueryTables,conn); 

This leads to this error: The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?) T4CustomFileGeneration这会导致此错误:找不到类型或命名空间名称“SqlConnection”(您是否缺少 using 指令或程序集引用?) T4CustomFileGeneration

I tried to add NuGet System.Data.SqlClient but that didn't solve anything.我尝试添加 NuGet System.Data.SqlClient 但这并没有解决任何问题。

What am I doing wrong?我究竟做错了什么?

Edit: After the tip off @Zer0 I've this:编辑:在@Zer0 的提示之后,我有这个:

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="EnvDTE"#>
<#@ assembly name="System.Core" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic"#>
<#@ import namespace="System.Linq"#>
<#@ import namespace="System.Text"#>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating"#>
<#@ output extension="/" #>
<#@ template language="C#v3.5" hostspecific="True"#>
<#@ include file="Manager.ttinclude"#>
using System.Data.SqlClient;

<#
    var manager = new Manager(Host, GenerationEnvironment, true, "T4CustomFileGeneration")
    {
        OutputPath = Path.GetDirectoryName(Host.TemplateFile),
    };
#>

<#
    string connectionString = ""; 
    System.Data.SqlClient.SqlConnection conn = new SqlConnection(connectionString); 
....

So that's both solutions but the errors are still there:所以这两种解决方案都存在,但错误仍然存在:

error

Full code:完整代码:

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="EnvDTE"#>
<#@ assembly name="System.Core" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic"#>
<#@ import namespace="System.Linq"#>
<#@ import namespace="System.Text"#>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating"#>
<#@ output extension="/" #>
<#@ template language="C#v3.5" hostspecific="True"#>
<#@ include file="Manager.ttinclude"#>
<#@ import namespace="System.Data.SqlClient"#>
<#@ import namespace="System.Data"#>

<#
    var manager = new Manager(Host, GenerationEnvironment, true, "T4CustomFileGeneration")
    {
        OutputPath = Path.GetDirectoryName(Host.TemplateFile),
    };
#>

<#
    string connectionString = ""; 
    System.Data.SqlClient.SqlConnection conn = new SqlConnection(connectionString); 
    string selectQueryTables = "select * from [config].[Tables]"; 
    SqlDataAdapter commandTables = new SqlDataAdapter(selectQueryTables,conn); 

    DataSet TablestoGenerate = new DataSet();
    commandTables.Fill(TablestoGenerate, "Tables");
    var manager = TemplateFileManager.Create(this);
#>

<# manager.StartBlock("FooStoredProc.sql", "Stored Procedures"); #>
CREATE PROC FOO ...
<# manager.EndBlock(); #>

<# manager.Process(); #>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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