Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlConnection stuck on Named Pipes

I'm seeing the following error when attempting to open a DB connection from within my C# application. I realize this error has probably shown up on 100's of questions before. However, in this scenario the error is only showing up on C# apps running on my specific desktop PC. I've scoured the internet via Google and within this website, but I can't find a solution.

ERROR:

"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)"

WHY THIS SCENARIO IS UNIQUE:

I am able to connect to the SQL server from SQL Server Management Studio (SSMS). This application works on another computer and is able to establish a connection to the SQL server from there. So this error scenario is specific to C# applications running on my specific desktop PC. Other apps work (SSMS). Other PC's work.

When I "sniff the wire" using WireShark, the trace shows me that my app is trying to connect via NamedPipes (i.e. \Server\IPC$). I can't seem to force it to use TCP/IP.

THINGS I'VE TRIED:

  • Re-installed.NET Framework
  • Re-installed Visual Studio (C# - Express version 2010)
  • Created an alias within cliconfg.exe.

Is there something I missed?

Here are Connection Strings I've tried...

Data Source=<servername>;Initial Catalog=HIE;Integrated Security=true
Server=tcp:10.240.11.81;Integrated Security=SSPI; database=HIE
Data Source=10.240.11.81,1433;Network Library=DBMSSOCN;Initial Catalog=HIE;User ID=<SqlUserIdThatISetUpAsSysAdmin>;Password=<password>

Here's the code snippet:

_conn = new SqlConnection();    // _conn declared globally
_conn.ConnectionString = <the connection string above>;
_conn.Open();
like image 330
Jay Lee Avatar asked Jul 11 '13 22:07

Jay Lee


People also ask

Do you need to close SqlConnection?

If the SqlConnection goes out of scope, it won't be closed. Therefore, you must explicitly close the connection by calling Close or Dispose . Close and Dispose are functionally equivalent. If the connection pooling value Pooling is set to true or yes , the underlying connection is returned back to the connection pool.

Does named pipes need to be enabled?

Yes, we need to enable named pipes and we need to give the port number 1433 in all the sections. It is mandatory step and it is required to establish the SQL connection.

Does using SqlConnection open connection?

Using a SqlConnection. As shown in Listing 1, you open a connection by calling the Open() method of the SqlConnection instance, conn. Any operations on a connection that was not yet opened will generate an exception. So, you must open the connection before using it.


1 Answers

The most likely scenario is that Windows Firewall is block the SQL Server communication. From MSDN (an article about named pipes, but relevant nonetheless):

Microsoft Windows XP Service Pack 2 enables Windows Firewall, which closes port 445 by default. Because Microsoft SQL Server communicates over port 445, you must reopen the port if SQL Server is configured to listen for incoming client connections using named pipes. For information on configuring a firewall, see "How to: Configure a Firewall for SQL Server Access" in SQL Server Books Online or review your firewall documentation.

Another scenario is that the client configuration on the local machine is not configured correctly. From the run prompt, you can execute cliconfg (the SQL Server Client Configuration Utility) to see the enabled protocols and the aliases that are used for the protocols.

like image 133
competent_tech Avatar answered Nov 14 '22 18:11

competent_tech