I am developing a C# Console Application targeted to .NET 4.0 (soon to be 4.5) using Visual Studio Pro 2013, connecting to a remote SQL Server. SQL Server connection string is defined in the constructor.
I have using the following code which used to work flawlessly but in the last week, as started to act in the following way:
If I recompile my code, the .Open()
call hangs. If I stop the application and restart it, it works ! Every time!
How can I debug what is hanging? If I stop it and re-run it, I can run 20 times without hanging. But if I recompile and run, it hangs. Then stop and re-run... works like a charm. I'm so confused :)
public static DataTable GetDataTableSql(string SqlCommand)
{
DataSet _data = new DataSet();
try
{
using (SqlConnection _connection = new SqlConnection(s_connectionString))
{
if (_connection.State != ConnectionState.Open)
{
_connection.Open();
}
}
}
}
Any pointers would be greatly appreciated!
-Ed
UPDATE: I was able to recover an error message from the .Open()
call:
GetDataTableSQL failed: Select * from tblSettings, 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)
Note that this ALWAYS HAPPENS IMMEDIATELY AFTER A RECOMPILE AND HAS NO ERROR IF I STOP AND RESTART IT ?!?!?!?
UPDATE: Connection string:
Data Source=X.X.X.X ;Initial Catalog=Catalog1;Persist Security Info=True;Connect Timeout=300;User ID=UserID;Password=Password1;