Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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);
            Console.WriteLine(srv1.Information.Version); 

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);
        Console.WriteLine(srv1.Information.Version);

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?

Edit:

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;

share|improve this question
add comment

1 Answer

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?

share|improve this answer
    
I get a different error. See edits above. –  rboarman Apr 24 '12 at 17:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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