c# - SQL Server CLR 与第 3 方 SDK 集成 : Error on missing assembly System. 绘图

标签 c# .net visual-studio-2013 sql-server-2014 sqlclr

我正在开发一个解决方案,它将与第 3 方应用程序的 SDK 接口(interface),并最终创建一个数据集以将数据迁移到我们 SQL Server 2014 实例上的目标数据库中。该接口(interface)是通过 SQL Server CLR 存储过程实现的。在 C# 端创建 CLR 程序集时使用了以下引用资料:

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.SqlTypes;
    using Microsoft.SqlServer.Server;
    using (3rd party SDK);
    using System.Dynamic;

代码构建正确且全部。但是,当我尝试使用 CREATE ASSEMBLY 语句创建程序集时,出现以下错误:

Assembly 'MyAssembly' references assembly 'system.drawing, version=4.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a.', which is not present in the current database. SQL Server attempted to locate and automatically load the referenced assembly from the same location where referring assembly came from, but that operation has failed (reason: 2(The system cannot find the file specified.)). Please load the referenced assembly into the current database and retry your request.

更进一步,如果我尝试基于明显缺失的引用 system.drawing 创建一个不安全的程序集(请记住,我确实在目标数据库中将 trustworthy 设置为 true),我会收到以下错误:

CREATE ASSEMBLY for assembly 'System.Drawing' failed because assembly 'System.Drawing' failed verification. Check if the referenced assemblies are up-to-date and trusted (for external_access or unsafe) to execute in the database. CLR Verifier error messages if any will follow this message
[ : System.Drawing.Image::Finalize][mdToken=0x600000d][offset 0x00000000] Code size is zero. (etc, etc. same error repeating on different methods in system.drawing)

我已经拥有匹配 v4.0 的解决方案和目标 SQL Server 数据库之间的 .NET 框架。老实说,我在这里不知所措,这似乎比应该的要难得多。我什至没有直接引用 System.Drawing,但出于某些奇怪的原因我需要它。

最佳答案

虽然它可能并不总是按预期运行,但您应该能够加载 System.Drawing进入 SQL Server。您需要将数据库设置为 TRUSTWORTHY ON (看起来你有),你需要将 Assembly 标记为 UNSAFECREATE ASSEMBLY语句(即使用 WITH PERMISSION_SET = UNSAFE 子句)。

如果您仍然遇到错误,请确保您正在加载实际的 System.Drawing DLL 而不是引用程序集之一,并确保您获取正确的 32 位或 64 位版本。

对于 CLR 4.0 版(由 SQL Server 2012 和更新版本使用),程序集路径应该是以下两个选项之一,具体取决于您需要 32 位还是 64 位版本:

  • C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Drawing.dll

  • C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Drawing.dll


此外,只要您使用的是绑定(bind)到正确 CLR 版本(即 4.0)的 .NET Framework 版本,您就不必担心。也就是说,您也可以使用 .NET Framework 4.5.2 或 4.6 版,只要目标服务器已更新到该 Framework 版本即可。

关于c# - SQL Server CLR 与第 3 方 SDK 集成 : Error on missing assembly System. 绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34323388/

相关文章:

php - Cakephp + 用于 PHP 的 Visual Studio 工具

c# - 我是否需要序列化抽象基类以使派生类可序列化

c# - 如何将自定义 css 样式表添加到 Umbraco Admin

c# - 从 C# 调用 ruby​​ 脚本

.net - 有什么方法可以重新启动线程吗?

c# - 是否可以在 Visual Studio 2013 中的 Web 性能测试之间传递参数?

c# - 在 Javascript 中处理 ActiveX 控件异常 (C#)

c# - 如何导入带有 int** 和 double** 参数的 C++ 函数

c# - 使用自定义 XmlMediaTypeFormatter 删除 web api 响应中的所有命名空间 c# .net

visual-studio-2013 - 如何在 Visual Studio 2013 数据库项目中添加新架构