Rajesh Subramanian
Rajesh Subramanian

Reputation: 6490

What is the default network protocol in SqlConnection class

In following code snippet, What will be the network protocol used to connect to the SQL Server? TCP/IP or Named Pipes or others?

using System;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
    //
    // First access the connection string.
    // ... This may be autogenerated in Visual Studio.
    //
    string connectionString = "Server=SERVER\\INSTANCE;Database=myDataBase;User Id=myUsername;
Password=myPassword;"
    //
    // In a using statement, acquire the SqlConnection as a resource.
    //
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        //
        // Open the SqlConnection.
        //
        con.Open();
        //
        // The following code uses an SqlCommand based on the SqlConnection.
        //
        using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", con))
        using (SqlDataReader reader = command.ExecuteReader())
        {
        while (reader.Read())
        {
            Console.WriteLine("{0} {1} {2}",
            reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
        }
        }
    }
    }
}

Upvotes: 4

Views: 12109

Answers (2)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239646

According to the SQL Server Native Client Configuration:

Protocols are tried in the order listed, attempting to connect using the top protocol first, and then the second listed protocol, etc.

BUT, we also read:

These settings are not used by Microsoft .NET SqlClient. The protocol order for .NET SqlClient is first TCP, and then named pipes, which cannot be changed.

So that is the order that they'll be attempted - TCP first, then Named Pipes - and so there's not "a" protocol that will be used - it depends on what succeeds.

Upvotes: 11

Rahul Tripathi
Rahul Tripathi

Reputation: 172408

From the MSDN

The .NET Framework Data Provider for SQL Server uses its own protocol to communicate with SQL Server. Therefore, it does not support the use of an ODBC data source name (DSN) when connecting to SQL Server because it does not add an ODBC layer.

And also this MSDN

If you specify a port number other than 1433 when you are trying to connect to an instance of SQL Server and using a protocol other than TCP/IP, the Open method fails. To specify a port number other than 1433, include "server=machinename,port number" in the connection string, and use the TCP/IP protocol.

Upvotes: 2

Related Questions