Why does this work to connect to my remote SQL Server database:

            var srvConn = new ServerConnection("sql01");
            srvConn.LoginSecure = false;  
            srvConn.Login = "unittests";
            srvConn.Password = "mypwd";
            srvConn.DatabaseName = "MediaFilesDb";

            var srv1 = new Server(srvConn);

And this does not work:

        var scsb = new SqlConnectionStringBuilder();
        scsb.DataSource = "sql01";
        scsb.InitialCatalog = "MediaFilesDb";
        scsb.IntegratedSecurity = false;
        scsb.UserID = "unittests";
        scsb.Password = "mypwd";

        var srv1 = new Server(scsb.ConnectionString);

I get the infamous error 40:

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)

If I take the connection string that is being generated and paste it into another tool, such as CodeSmith, it works correctly and the connection is established.

Why does the second example fail?


If I specify TCP/IP for the network library:

scsb.NetworkLibrary = "dbmssocn";

I get this error:

The value's length for key 'data source' exceeds it's limit of '128'.

This is my connection string:

Data Source=sql01;Initial Catalog=MediaFilesDb;Integrated Security=False;User ID=unittests;Password=****;Pooling=False;Network Library=dbmssocn;


Is the SQL instance on the local machine or a different one? Have you tried changing the NetworkLibrary property of the SqlConnection to make it use TCP/IP instead of Named Pipes?

  • I get a different error. See edits above. – rboarman Apr 24 '12 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.