2014-08-28 24 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,然后命名管道 - 所以有没有“一”协议,将使用 - 这取决于什么成功