无法通过 T4 模板访问我的 SQL Server 2008 错误返回给我如何获取表?

发布于 2024-12-17 16:52:30 字数 8247 浏览 0 评论 0原文

我想使用 T4 模板生成一些代码。我使用“sa”和 sa 的密码进行连接。但我无法连接到远程 SQL Server。我正在使用 SQL Server 2008。

错误如下:

运行转换时出错:Microsoft.SqlServer.Management.Smo.FailedOperationException:SetParent 数据库“FlyAnt”失败。 --->
Microsoft.SqlServer.Management.Common.ConnectionFailureException: 无法连接到服务器 MROAPPSQL。 --->
System.Data.SqlClient.SqlException:用户登录失败 'MROTECHNIC\yusuf.karatoprak'。
在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常,布尔值breakConnection)
在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
在System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSetbulkCopyHandler,TdsParserStateObject 状态对象)
在 System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(布尔 入伍确定)
在 System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo、字符串 newPassword、布尔值ignoreSniOpenTimeout、 TimeoutTimer超时,SqlConnection所属对象)
在 System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo、字符串 newPassword、布尔值重定向用户实例、 SqlConnection owningObject、SqlConnectionString 连接选项、 Timeout定时器超时)
在 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject、TimeoutTimer超时、SqlConnectionString 连接选项、字符串新密码、布尔值重定向用户实例)
在 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity 身份、SqlConnectionString 连接选项、对象提供者信息、 字符串 newPassword、SqlConnection owningObject、布尔值 重定向用户实例)
在 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions 选项、对象 poolGroupProviderInfo、DbConnectionPool 池、 DbConnection 拥有Connection)
在 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection、DbConnectionPool 池、DbConnectionOptions 选项)
在 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection 拥有对象)
在 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection 拥有对象)
在 System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection 拥有对象)
在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection 拥有连接)
在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection 外部连接、DbConnectionFactory 连接工厂)
在 System.Data.SqlClient.SqlConnection.Open()
在 Microsoft.SqlServer.Management.Common.ConnectionManager.InternalConnect(WindowsIdentity 模拟身份)
在 Microsoft.SqlServer.Management.Common.ConnectionManager.Connect()
--- 内部异常堆栈跟踪结束 ---
在 Microsoft.SqlServer.Management.Common.ConnectionManager.Connect()
在 Microsoft.SqlServer.Management.Common.ConnectionManager.PoolConnect()
在 Microsoft.SqlServer.Management.Common.ConnectionManager.get_ServerVersion()
在 Microsoft.SqlServer.Management.Smo.ExecutionManager.GetServerVersion()
在 Microsoft.SqlServer.Management.Smo.SqlSmoObject.get_ServerVersion()
在 Microsoft.SqlServer.Management.Smo.SqlSmoObject.ValidateParent(SqlSmoObject 新父母)
在Microsoft.SqlServer.Management.Smo.SqlSmoObject.SetParentImpl(SqlSmoObject 新父母)
--- 内部异常堆栈跟踪结束 --- 在Microsoft.SqlServer.Management.Smo.SqlSmoObject.SetParentImpl(SqlSmoObject 新父母)
在 Microsoft.SqlServer.Management.Smo.Database..ctor(服务器服务器,字符串名称)
在 Microsoft.VisualStudio.TextTemplate3B02A40A7B5CFF5BC85C47D46E8EE44A.GeneeratedTextTransformation.CodeGenerator.RunCore()
在 T4Toolbox.Generator.Run()
在 Microsoft.VisualStudio.TextTemplate3B02A40A7B5CFF5BC85C47D46E8EE44A.GenerateTextTransformation.TransformText()
在 Microsoft.VisualStudio.TextTemplate.TransformationRunner.RunTransformation(TemplateProcessingSession 会话、字符串源、ITextTemplatedEngineHost 主机、字符串& 结果)

我的代码是这样的:

<#@ template language="C#"#>
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" #>
<#@ assembly name="Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" #>
<#@ assembly name="Microsoft.SqlServer.Management.Sdk.sfc, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
<#@ import namespace="Microsoft.SqlServer.Management.Common" #>
<#@ assembly name="System.Xml"#>


<#@ import namespace="System.Reflection"#>


   <#+
    public class CodeTemplate : Template
    {
        public string DatabaseName;
        public string ServerName;
        public string TableName;

        public override string TransformText()
        {
            Server server = new Server(ServerName);
                server.ConnectionContext.LoginSecure = false;

                server.ConnectionContext.Login = "sa";
                server.ConnectionContext.Password = "123456";
            server.ConnectionContext.Connect();
            Database database = new Database(server, DatabaseName);
            Table table = new Table(database, TableName);
            table.Refresh();

                string DataType= string.Empty;
            WriteLine("using System;\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\n");

            WriteLine("namespace MyCustomEntities\n{");

            WriteLine("//Select");
                WriteLine("\tpublic partial class My" +table.Name+ "\n\t{");
            GenerateProperties(table);
                   //output the contructor
            WriteLine("\n\t\tpublic My" + table.Name + "()\n\t\t{\n\t\t//constructor\n\t\t}" );

            //end class
            WriteLine("\t}");
              WriteLine("//Update Delete Save");
                WriteLine("\tpublic partial class My" +table.Name+ "\n\t{");

                WriteLine("\t}");
                WriteLine("}");
            return this.GenerationEnvironment.ToString();
          }

        protected void GenerateProperties( Table table)
        {
                 foreach (Column  column in table.Columns)
                {
                    switch(column.DataType.Name)
                    {
                        case "nvarchar":
                        case "varchar":
                        case "nchar":
                        case "char":
                            WriteLine("\t\tpublic string "+column.Name+"{ get; set;}");
                            break;
                        case "int":
                        case "smallint":
                        case "bigint":
                            WriteLine("\t\tpublic int "+column.Name+" { get; set;}");
                            break;
                        case "datetime":
                            WriteLine("\t\tpublic DateTime "+column.Name+" { get; set;}");
                            break;
                        case "image":
                            WriteLine("\t\tpublic byte[] "+column.Name+" { get; set;}");
                            break;
                        case "bit":
                            WriteLine("\t\tpublic bool "+column.Name+" { get; set;}");
                            break;
                    }
            }
        }
    }

   #>

我的连接代码:

<#@ template language="C#" hostspecific="True"  #>
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" #>
<#@ assembly name="Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" #>
<#@ assembly name="Microsoft.SqlServer.Management.Sdk.sfc, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
<#@ import namespace="Microsoft.SqlServer.Management.Common" #>
<#@ assembly name="System.Xml"#>
<#@ output extension="txt" #>
<#@ import namespace="System.Reflection"#>

<#@ include file="T4Toolbox.tt" #> 
<#@ include file="CodeGenerator.tt" #> 

<# 
    CodeGenerator gen = new CodeGenerator();

    gen.ServerName="MYSQLSERVER";
    gen.DatabaseName="FlyAnt";
    gen.Run();


#>

I want to generate some code using a T4 template. I am connecting using "sa" and sa's password. But I cannot connect to a remote SQL Server. I am using SQL Server 2008.

Error is below :

Error Running transformation: Microsoft.SqlServer.Management.Smo.FailedOperationException: SetParent
failed for Database 'FlyAnt'. --->
Microsoft.SqlServer.Management.Common.ConnectionFailureException:
Failed to connect to server MROAPPSQL. --->
System.Data.SqlClient.SqlException: Login failed for user
'MROTECHNIC\yusuf.karatoprak'.
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject
stateObj)
at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean
enlistOK)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo
serverInfo, String newPassword, Boolean ignoreSniOpenTimeout,
TimeoutTimer timeout, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo
serverInfo, String newPassword, Boolean redirectedUserInstance,
SqlConnection owningObject, SqlConnectionString connectionOptions,
TimeoutTimer timeout)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection
owningObject, TimeoutTimer timeout, SqlConnectionString
connectionOptions, String newPassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity
identity, SqlConnectionString connectionOptions, Object providerInfo,
String newPassword, SqlConnection owningObject, Boolean
redirectedUserInstance)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions
options, Object poolGroupProviderInfo, DbConnectionPool pool,
DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection
owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection
owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection
owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection
owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection
owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection
outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at Microsoft.SqlServer.Management.Common.ConnectionManager.InternalConnect(WindowsIdentity
impersonatedIdentity)
at Microsoft.SqlServer.Management.Common.ConnectionManager.Connect()
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Common.ConnectionManager.Connect()
at Microsoft.SqlServer.Management.Common.ConnectionManager.PoolConnect()
at Microsoft.SqlServer.Management.Common.ConnectionManager.get_ServerVersion()
at Microsoft.SqlServer.Management.Smo.ExecutionManager.GetServerVersion()
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.get_ServerVersion()
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.ValidateParent(SqlSmoObject
newParent)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.SetParentImpl(SqlSmoObject
newParent)
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.SetParentImpl(SqlSmoObject
newParent)
at Microsoft.SqlServer.Management.Smo.Database..ctor(Server server, String name)
at Microsoft.VisualStudio.TextTemplating3B02A40A7B5CFF5BC85C47D46E8EE44A.GeneratedTextTransformation.CodeGenerator.RunCore()
at T4Toolbox.Generator.Run()
at Microsoft.VisualStudio.TextTemplating3B02A40A7B5CFF5BC85C47D46E8EE44A.GeneratedTextTransformation.TransformText()
at Microsoft.VisualStudio.TextTemplating.TransformationRunner.RunTransformation(TemplateProcessingSession
session, String source, ITextTemplatingEngineHost host, String&
result)

My code is this:

<#@ template language="C#"#>
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" #>
<#@ assembly name="Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" #>
<#@ assembly name="Microsoft.SqlServer.Management.Sdk.sfc, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
<#@ import namespace="Microsoft.SqlServer.Management.Common" #>
<#@ assembly name="System.Xml"#>


<#@ import namespace="System.Reflection"#>


   <#+
    public class CodeTemplate : Template
    {
        public string DatabaseName;
        public string ServerName;
        public string TableName;

        public override string TransformText()
        {
            Server server = new Server(ServerName);
                server.ConnectionContext.LoginSecure = false;

                server.ConnectionContext.Login = "sa";
                server.ConnectionContext.Password = "123456";
            server.ConnectionContext.Connect();
            Database database = new Database(server, DatabaseName);
            Table table = new Table(database, TableName);
            table.Refresh();

                string DataType= string.Empty;
            WriteLine("using System;\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\n");

            WriteLine("namespace MyCustomEntities\n{");

            WriteLine("//Select");
                WriteLine("\tpublic partial class My" +table.Name+ "\n\t{");
            GenerateProperties(table);
                   //output the contructor
            WriteLine("\n\t\tpublic My" + table.Name + "()\n\t\t{\n\t\t//constructor\n\t\t}" );

            //end class
            WriteLine("\t}");
              WriteLine("//Update Delete Save");
                WriteLine("\tpublic partial class My" +table.Name+ "\n\t{");

                WriteLine("\t}");
                WriteLine("}");
            return this.GenerationEnvironment.ToString();
          }

        protected void GenerateProperties( Table table)
        {
                 foreach (Column  column in table.Columns)
                {
                    switch(column.DataType.Name)
                    {
                        case "nvarchar":
                        case "varchar":
                        case "nchar":
                        case "char":
                            WriteLine("\t\tpublic string "+column.Name+"{ get; set;}");
                            break;
                        case "int":
                        case "smallint":
                        case "bigint":
                            WriteLine("\t\tpublic int "+column.Name+" { get; set;}");
                            break;
                        case "datetime":
                            WriteLine("\t\tpublic DateTime "+column.Name+" { get; set;}");
                            break;
                        case "image":
                            WriteLine("\t\tpublic byte[] "+column.Name+" { get; set;}");
                            break;
                        case "bit":
                            WriteLine("\t\tpublic bool "+column.Name+" { get; set;}");
                            break;
                    }
            }
        }
    }

   #>

My connection code :

<#@ template language="C#" hostspecific="True"  #>
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" #>
<#@ assembly name="Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" #>
<#@ assembly name="Microsoft.SqlServer.Management.Sdk.sfc, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
<#@ import namespace="Microsoft.SqlServer.Management.Common" #>
<#@ assembly name="System.Xml"#>
<#@ output extension="txt" #>
<#@ import namespace="System.Reflection"#>

<#@ include file="T4Toolbox.tt" #> 
<#@ include file="CodeGenerator.tt" #> 

<# 
    CodeGenerator gen = new CodeGenerator();

    gen.ServerName="MYSQLSERVER";
    gen.DatabaseName="FlyAnt";
    gen.Run();


#>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2024-12-24 16:52:30

异常消息表明您正在尝试使用 Windows 帐户登录,该帐户是正在运行的进程的标识

System.Data.SqlClient.SqlException:用户登录失败
'MROTECHNIC\yusuf.karatoprak'

如果您查看堆栈跟踪,您会发现失败的连接尝试实际上源自 以 Server 对象作为参数的 Database 类的构造函数

at Microsoft.SqlServer.Management.Common.ConnectionManager.Connect()
[...]
at Microsoft.SqServer.Management.Smo.Database..ctor(Server server, String name)
[...]
at T4Toolbox.Generator.Run()

似乎生成过程是由 T4 工具箱

The exception message states that you're attempting to login using a Windows account, which is the identity of the running process:

System.Data.SqlClient.SqlException: Login failed for user
'MROTECHNIC\yusuf.karatoprak'

If you look at the stack trace, you'll find that the failed connection attempt actually originates from the constructor of the Database class that takes the Server object as argument:

at Microsoft.SqlServer.Management.Common.ConnectionManager.Connect()
[...]
at Microsoft.SqServer.Management.Smo.Database..ctor(Server server, String name)
[...]
at T4Toolbox.Generator.Run()

It also seems that the generation process is being kicked off by T4 Toolbox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文