Liar, Liar, Pants On Fire:Named Pipes Provider, error: 40 - Could not open a connection to SQL Server

I am building a tiny toolset for my application to do some database deployments and such. I thought I’d use Sql Server Management Objects (SMO) to consume some sql scripts and NHibernate output manually.

First, I needed to make sure remote connections were enabled. Here are some links to help with that:

The last one is a great link to lots of resources to check for when connections can’t be made remotely to sql server. Also this shows enabling the SMO stored procs.

After making sure my settings were correct and restarting the server, I referenced microsoft.sqlserver.connectioninfo.dll in my project.

Then I wrote this code:

string sqlConnectionString = "Data Source=(local);Initial Catalog=MyDb;Integrated Security=True";
FileInfo file = new FileInfo("create_db.sql");
string script = file.OpenText().ReadToEnd();
Server server = new Server(new ServerConnection(sqlConnectionString));
server.ConnectionContext.ExecuteNonQuery(script);

 

Can you spot the problem?

Server server = new Server(new ServerConnection(sqlConnectionString));

This expects to be handed a connection not a connection string, but the exception in this case sent me on a wild goose chase wondering whether my server was somehow configured incorrectly or my connection string was bad.

While it is true this was careless on my part, the exception that resulted here was just plain misleading and the stack trace didn’t provide any real hints either. This makes me want to be more diligent in relaying meaningful exceptions at appropriate times in my own code.


Posted 01-11-2010 11:02 AM by Michael Nichols
Filed under: ,

[Advertisement]

Comments

Robin Clowers wrote re: Liar, Liar, Pants On Fire:Named Pipes Provider, error: 40 - Could not open a connection to SQL Server
on 01-11-2010 4:26 PM

Maybe I am missing something but why did that compile?  If the ServerConnection constructor takes a SqlConnection, then how could you pass a string?

Michael Nichols wrote re: Liar, Liar, Pants On Fire:Named Pipes Provider, error: 40 - Could not open a connection to SQL Server
on 01-11-2010 5:08 PM

One of the overloads accepts a string but it is for a named instance I believe...I should have mentioned that :)

Bill Pierce wrote re: Liar, Liar, Pants On Fire:Named Pipes Provider, error: 40 - Could not open a connection to SQL Server
on 01-12-2010 11:23 AM

Useful error messages Mike?  Come on, be reasonable buddy.

Michael Nichols wrote re: Liar, Liar, Pants On Fire:Named Pipes Provider, error: 40 - Could not open a connection to SQL Server
on 01-12-2010 11:49 AM

@Bill, :)

You're right...Here's what I am doing now in all my code:

throw new WillyWonkaException("Please proceed to google and try to figure out what is wrong with your code");

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)