【问题标题】:What is the default network protocol in SqlConnection classSqlConnection 类中的默认网络协议是什么
【发布时间】:2014-10-21 20:55:41
【问题描述】:

在下面的代码 sn-p 中,用于连接 SQL Server 的网络协议是什么? TCP/IP 或命名管道或其他?

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

【问题讨论】:

    标签: c# asp.net .net sql-server ado.net


    【解决方案1】:

    来自MSDN

    用于 SQL Server 的 .NET Framework 数据提供程序使用自己的协议 与 SQL Server 通信。所以不支持使用 连接到 SQL Server 时的 ODBC 数据源名称 (DSN) 因为它没有添加 ODBC 层。

    还有这个MSDN

    如果您在尝试使用 1433 以外的端口号 连接到 SQL Server 实例并使用除 TCP/IP,Open 方法失败。指定端口号 1433,在连接中包含“server=machinename,port number” 字符串,并使用 TCP/IP 协议。

    【讨论】:

    • 所以默认端口号是 1433,协议是 TCP/IP?
    • @RajeshSubramanian - 它会尝试首先使用 TCP,但可能会退回到命名管道。
    【解决方案2】:

    根据 SQL Server Native Client Configuration

    按照列出的顺序尝试协议,首先尝试使用顶级协议进行连接,然后是列出的第二个协议,依此类推。

    但是,我们还读到:

    Microsoft .NET SqlClient 不使用这些设置。 .NET SqlClient的协议顺序是先TCP,后命名管道,不能更改。

    这就是它们将被尝试的顺序 - 首先是 TCP,然后是命名管道 - 因此不会使用“一个”协议 - 这取决于 成功。 p>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-26
      • 2021-01-23
      • 1970-01-01
      • 2015-02-03
      • 2017-12-23
      • 1970-01-01
      • 2015-05-12
      • 2014-01-03
      相关资源
      最近更新 更多