简体   繁体   English

为什么LinqPad可以在我的控制台应用程序中连接Db。 抛出相同代码的SQL异常?

[英]Why LinqPad can connect the Db while my console app. throws an SQL Exception for the same code?

I run the code below 我运行下面的代码

var connectionString="Data Source=MyFooServer.myDomaion.com;Initial Catalog=FooDb;Integrated Security=True; MultipleActiveResultSets=true";

using (var con = new SqlConnection(connectionString))
{
    var q = "SELECT COUNT(*) FROM Pizza";
    SqlCommand cmd = new SqlCommand();
    SqlDataReader reader;
    cmd.CommandText = q;

    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    con.Open();

    reader = cmd.ExecuteReader();
    con.Close();
}

from 2 different places: 来自2个不同的地方:

1) LinqPad 1)LinqPad

2) A Console application in Visual Studio 2010 2)Visual Studio 2010中的控制台应用程序

LinqPad works fine. LinqPad工作正常。 No exceptions. 没有例外。 But the same code throws an SQL exception : "The target principal name is incorrect. Cannot generate SSPI context." 但是同一代码会引发SQL异常“目标主体名称不正确。无法生成SSPI上下文。”

QUESTION: Why I can connect to my database without any problem from LinqPad but not from the console application even tough I run the same code? 问题:为什么即使我运行相同的代码也很难从LinqPad连接到数据库,但从控制台应用程序却没有问题,为什么?

重新启动我的机器有帮助...不知道造成此行为困难的根本原因是什么。

尝试使用SQL用户名和密码代替Windows Inegrated Security。

var connctionString="Data Source=MyFooServer.myDomain.com;Initial Catalog=FooDb;Uid=Username; pwd=password"

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

相关问题 为什么我的代码可以作为控制台应用程序成功运行,但是会作为类库引发异常? C#Microsoft.Exchange.Webservices - Why does my code run successfully as a console app, but throws an exception as a class library? C# Microsoft.Exchange.Webservices 为什么 ReadKey 在从 Git Bash 运行 .net-core 控制台应用程序时会引发异常? - Why ReadKey throws exception while running a .net-core console app from Git Bash? 为什么它在LINQPad中有效但在我的代码中无效? - Why does it work in LINQPad but not in my code? .Except LINQ 在 MVC 中构建时不排除值,但在简单的控制台应用程序中排除值。 为什么? - .Except LINQ does not exclude values while building in MVC, but does in a simple console app. Why? LINQPad在有效代码上引发NullReferenceException - LINQPad throws NullReferenceException on valid code 我的c#代码在访问sql server时抛出异常 - my c# code throws an exception on accessing sql server 如何从Windows应用商店应用连接到SQL Azure数据库表? - How can I connect to a SQL Azure DB table from my Windows store app? LinqPad SQL Compact Edition异常 - LinqPad SQL Compact Edition Exception 某些操作正在删除控制台应用程序。 为什么要删除它? - Something is deleting a console app. Why is it being deleted? 网站无法连接到SQL Server,但控制台应用程序可以 - Website can't connect to SQL Server, but console app can
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM