You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级Microsoft.Data.SqlClient至5.0.0后创建ServerConnection报错咨询

问题描述

当前使用版本:

  • Microsoft.SqlServer.SqlManagementObjects 161.47027.0
  • Microsoft.Data.SqlClient 5.0.0

将Microsoft.Data.SqlClient从4.1.0升级至5.0.0后,创建ServerConnection对象时出现如下错误:

代码示例:

using Microsoft.SqlServer.Management.Common;

var serverConnection = new ServerConnection("localhost", "sa", "mypwd");

错误信息:

System.MissingMethodException: 'Method not found: 'Void Microsoft.Data.SqlClient.SqlConnectionStringBuilder.set_Encrypt(Boolean)

错误显示程序在Microsoft.Data.SqlClient.SqlConnectionStringBuilder中寻找未实现的set_Encrypt方法。

请问这是否意味着Microsoft.SqlServer.SqlManagementObjects尚未更新以支持Microsoft.Data.SqlClient 5.0.0?如果是这样,我是否需要等待该组件更新后才能升级Microsoft.Data.SqlClient至5.0.0?


回答

是的,你遇到的问题核心就是Microsoft.SqlServer.SqlManagementObjects (SMO) 161.47027.0版本未适配Microsoft.Data.SqlClient 5.0.0。

原因说明

Microsoft.Data.SqlClient 5.0.0对SqlConnectionStringBuilder.Encrypt属性做了重大变更:将原本的bool类型替换为SqlConnectionEncryptOption枚举类型。而你使用的旧版SMO仍然在尝试调用基于bool参数的set_Encrypt方法,由于该方法在SqlClient 5.0.0中已不存在,因此抛出MissingMethodException。

解决方案

  1. 升级SMO到兼容版本
    162.x及以后的SMO版本已经完成了对Microsoft.Data.SqlClient 5.x系列的适配,直接升级SMO到对应版本即可解决问题。

  2. 回退SqlClient版本
    如果没有必须使用SqlClient 5.0.0新特性的需求,暂时回退到Microsoft.Data.SqlClient 4.1.0版本是最稳妥的方案,等SMO的兼容版本验证稳定后再升级。

  3. 临时绑定重定向(不推荐生产环境)
    可以在项目的配置文件(app.config/web.config)中添加绑定重定向,强制SMO使用你项目中安装的SqlClient版本,但这种方式可能引发其他兼容性问题,仅建议用于测试场景:

    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="Microsoft.Data.SqlClient" publicKeyToken="23ec7fc2d6eaa4a5" culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
        </dependentAssembly>
      </assemblyBinding>
    </runtime>
    

内容的提问来源于stack exchange,提问作者Ghini Antonio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 15:27:21