首页 > 解决方案 > 如何在 SQL Server 2014 中为 SQL CLR 注册 PdfSharp?

问题描述

我有一个 CLR 存储过程,它调用 PdfSharp 1.32.3057.0 从给定查询创建 PDF。PdfSharp 在本地开发服务器(Windows 2012 / SQL Server 2014)上注册良好,但在 QA 服务器(Windows 2008 r2 / SQL Server 2014)上注册不正常。

已尝试在 SA 帐户下注册 pdfsharp,该帐户是 dbo for 'util' db。

CREATE ASSEMBLY [PdfSharp] 
           FROM 'c:\SqlClr\PdfSharp.dll'
WITH PERMISSION_SET = UNSAFE

警告:Microsoft .NET Framework 程序集 'system.windows.forms,version=2.0.0.0,culture=neutral,publickeytoken=b77a5c561934e089,processorarchitecture=msil。' 您注册的内容未在 SQL Server 托管环境中进行全面测试,因此不受支持。将来,如果您升级或维修此程序集或 .NET Framework,您的 CLR 集成例程可能会停止工作。有关详细信息,请参阅 SQL Server 联机丛书。

消息 6586,级别 16,状态 1,第 118 行
程序集“System.Windows.Forms”无法安装,因为现有策略会阻止它被使用

is_trustworthy为 util db 设置为 on。

.NET 4.7.2 和 .NET 3.5.1 都安装在 QA 服务器上。

如何正确注册此程序集?

标签: c#sql-serversqlclr

解决方案


1) This is a bad idea. You should use an external program, perhaps simply PowerShell through SQL Agent to do this.

2) Here's how to do it anyway.

The assembly you're attempting to load references .NET framework assemblies not on the "blessed list" for SQL Server, which means that they are not tested or supported for running in-proc. And to use them you have to copy them into your database.

Once you copy .NET Framework assemblies in your database you must update them any time the .NET Framework version on the host changes (including patches and minor version changes), or your database runs on another server (eg failover). Someone will forget to do this and your app will crash.

SQL 2012 and later use .NET Framework 4. It's no big deal that the app was built against .NET 2; it will run in 4 (probably). And the .NET Framework 2 assembly references will be silently redirected to the .NET 4 versions. But this means that you must install the .NET 4 versions from the server into your database. The complete list you need to get System.Windows.Forms.dll working is:

system.windows.forms, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089, processorarchitecture=msil
system.drawing, version=4.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a, processorarchitecture=msil
accessibility, version=4.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a, processorarchitecture=msil
system.runtime.serialization.formatters.soap, version=4.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a, processorarchitecture=msil

These assemblies are all located in:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319

推荐阅读