您需要将对Microsoft.SqlSserver Smo DLL的适当版本的引用添加到项目中。
此代码中的以下项目需要替换为适合您环境的适当值:
戈谢尔数据库
ProjectNamespaceGoesher
<#@ 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());
}
}
#>