简体   繁体   English

重建后,C#控制台应用程序每次都无法连接SQL Server

[英]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.

相关问题 如何在插入大型数据集后首先以编程方式重建代码中的所有索引(C# 和 EntityFrameworkCore)创建的 SQL Server 表 - How to programmatically rebuild all indexes in code first (C# and EntityFrameworkCore) created SQL Server tables after inserting large dataset 如何在C#应用程序上连接到SQL Server - How to connect to sql server on c# application 如何将C#控制台应用程序与存储在.sql文件中的数据库连接 - How to connect a C# console application with a database stored in a .sql file 手动故障转移后,C#应用程序无法连接到镜像SQL Server - C# Application can't connect to mirroring SQL Server after manually failover 如何在第一次启动C#应用程序时将数据输入SQL Server - How to input data into SQL Server for first time start of C# application How would I allow a C# Application to connect to an Azure SQL database without having to add the IP address to the firewall every time - How would I allow a C# Application to connect to an Azure SQL database without having to add the IP address to the firewall every time 从 SQL Server 代理(作业)运行 C# 控制台应用程序? - Run a C# Console Application from SQL Server Agent (Job)? 无法从 C# 应用程序连接到远程 SQL Server 实例 - Unable to connect to remote SQL Server Instance from C# Application 将C#Windows应用程序连接到基于Web的Sql Server - Connect C# windows application to web based Sql server 无法从Windows应用程序C#连接到SQL Server数据库 - unable to connect to sql server database from windows application c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM