I have a C# console application in Main()
I have
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. I get the following exception.
A network-related or instance-specific error occurred while establishing a connection to 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. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server.
Weirdly, after the console close. If I double click the exe file again.
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.
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
<?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
SQL Server Enterprise x64
Try disabling your anti-virus program.
It could be causing problems eg scanning new programs before allowing them to access the network.