Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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));
        }
        }
    }
    }
}
like image 323
Rajesh Subramanian Avatar asked Oct 22 '25 18:10

Rajesh Subramanian


1 Answers

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.

like image 161
Damien_The_Unbeliever Avatar answered Oct 24 '25 07:10

Damien_The_Unbeliever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!