[英]C# Console application fail to connect SQL Server every first time after rebuild
I have a C# console application in Main()
I have 我的
Main()
有一个C#控制台应用程序
using (TransactionScope scope = new TransactionScope())
{
var connStr = ConfigurationManager.ConnectionStrings["HQMemberConnStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
// some modification to DB
}
scope.Complete();
}
Every time I rebuild the project and run the exe file by double click. 每次我重建项目并通过双击运行exe文件时。 I get the following exception.
我得到以下异常。
A network-related or instance-specific error occurred while establishing a connection to SQL Server.
建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible.
服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server.
(提供者:命名管道提供者,错误:40-无法打开与SQL Server的连接。
Weirdly, after the console close. 奇怪的是,控制台关闭后。 If I double click the exe file again.
如果我再次双击该exe文件。 It works fine since then.
从那时起,它运作良好。
Well, until I rebuild project again then I will have that exception at the first time I run the application again which is pretty annoying. 好吧,直到我再次重建项目,然后我才会在第一次运行该应用程序时遇到该异常,这很烦人。
Moreover, If I rebuild then run the application by Visual Studio 2013 it behaves correctly. 此外,如果我重新构建,然后通过Visual Studio 2013运行该应用程序,则其行为正确。
I have tested the comment part. 我已经测试了评论部分。 No matter I comment them or not It seem like it has nothing to do with the problem.
不管我是否评论,似乎都与问题无关。
I also close the firewall to check if it has something to do with the problem but didn't get lucky. 我还关闭了防火墙,以检查它是否与问题有关,但是没有走运。
I want to make it behave right every single time but I'm not sure what I did wrong. 我想让它每次都表现正确,但是我不确定自己做错了什么。
My App.config 我的App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="HQMemberConnStr"
connectionString="Server=.;Database=HQMember;User Id=sa;Password=****;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
My .net version is 4.5 我的.net版本是4.5
SQL Server Enterprise x64 SQL Server企业版x64
Try disabling your anti-virus program. 尝试禁用您的防病毒程序。
It could be causing problems eg scanning new programs before allowing them to access the network. 这可能会引起问题,例如在允许新程序访问网络之前对其进行扫描。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.