2014-08-28 29 views
1

在以下代碼片段中,將用於連接到SQL Server的網絡協議是什麼? TCP/IP或命名管道或其他?什麼是SqlConnection類中的默認網絡協議

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)); 
     } 
     } 
    } 
    } 
} 

回答

1

MSDN

.NET Framework數據提供程序SQL Server 使用自己的協議 與SQL Server進行通信。因此,當連接到SQL Server 時,它不支持ODBC數據源名稱(DSN)的使用 ,因爲它不添加ODBC層。

而且也是這個MSDN

如果您指定1433以外的端口號,當你試圖 連接到SQL Server的實例,並使用比 TCP/IP等協議,該打開方法失敗。要指定除 1433以外的端口號,請在連接 字符串中包含「server = machinename,port number」,並使用TCP/IP協議。

+0

所以默認的端口號是1433,協議是TCP/IP? – 2014-08-28 05:31:32

+0

@RajeshSubramanian - 它會*嘗試*先使用TCP,但可能會回退到命名管道。 – 2014-08-28 05:32:24

7
根據

到SQL Server本機客戶端Configuration

協議是試圖按照列出的順序,嘗試首先使用頂部協議的第二個列出的協議進行連接,然後等

,我們也讀到:

這些設置不被Microsoft .NET SqlClient使用。 .NET SqlClient的協議順序是第一個TCP,然後是命名管道,不能更改。

所以這是爲了他們將嘗試 - 第一TCP,然後命名管道 - 所以有沒有「一」協議,將使用 - 這取決於什麼成功