T4中使用SQLEXPRESS的一个问题

今天在学习T4时(T4: Text Template Transformation Toolkit),使用作者的代码,发生错误:

image 原因是我使用的的是SQLEXPRESS,将代码改为如下即可:

<#@ template language="C#v3.5" #>
<#@ output extension="SQL" #>
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
<#@ assembly name="Microsoft.SqlServer.Smo" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
<#
    Server server = new Server(@"PC-200903252132\SQLEXPRESS");
    Database database = new Database(server, "Northwind");
    Table table = new Table(database, "Products");
    table.Refresh();
#>
create procedure <#= table.Name #>_Delete 
<#
    PushIndent("\t");
    foreach (Column column in table.Columns)
    {
        if (column.InPrimaryKey)
            WriteLine("@" + column.Name + " " + column.DataType.Name);
    }
    PopIndent();
#>
as
    delete from <#= table.Name #>
    where
<#
    PushIndent("\t\t");
    foreach (Column column in table.Columns)
    {
        if (column.InPrimaryKey)
            WriteLine(column.Name + " = @" + column.Name);
    }
    PopIndent();
#>

另外,TT在保存时会自动编译、生成,运行时的错误可以将debug=”true”打开可以看到发生错误的位置,对我们初学者这点很重要。

posted @ 2011-07-03 22:08  GDLMO  阅读(406)  评论(0)    收藏  举报