无法在 SQL 中创建程序集
我已经深入研究 SQL clr 编程。不幸的是,我的第一次尝试遇到了麻烦。 我的 C# 汇编代码如下:
enter code here
public partial class FirstCLRRoutines
{
public static int GetCLRFrameworkMajorVersion()
{
return System.Environment.Version.Major;
}
}
SQL 代码为:
USE master
GO
CREATE ASSEMBLY [Chapter2.FirstCLRRoutine]
FROM 'D:\projeler\SQL_CLR\SQL_CLR\bin\Debug\SQL_CLR.dll'
但我从 MSSMSE 收到此错误消息:
消息 6218,16 级,状态 3,第 1 行
程序集“SQL_CLR”的 CREATE ASSEMBLY 失败,因为程序集“SQL_CLR”验证失败。检查引用的程序集是否是 最新且受信任(对于 external_access 或不安全)执行 数据库。 CLR 验证程序错误消息(如果有)将遵循此 留言
I've already dived into SQL clr programming. Unfortunately my first attempt is troubled.
My C# assembly code is just so:
enter code here
public partial class FirstCLRRoutines
{
public static int GetCLRFrameworkMajorVersion()
{
return System.Environment.Version.Major;
}
}
And SQL code is:
USE master
GO
CREATE ASSEMBLY [Chapter2.FirstCLRRoutine]
FROM 'D:\projeler\SQL_CLR\SQL_CLR\bin\Debug\SQL_CLR.dll'
But I get this error message from MSSMSE:
Msg 6218, Level 16, State 3, Line 1
CREATE ASSEMBLY for assembly 'SQL_CLR' failed because assembly 'SQL_CLR' 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我刚刚遇到了完全相同的问题。
这是一个旧页面,但首先,最重要的是,最后,DLL 必须使用 .NET 2.0 构建。 SqlServer 2005 是使用.net 2.0 构建的,因为它是编写时最新的可用版本。 SqlServer 2008 也可能有同样的问题。
DLL 和 SqlServer 必须引用相同的 .NET 框架版本。一旦我弄清楚了这一点,我就在 2.0 和 shazam! 下创建了一个单独的项目。第一次就完美地工作了。
要查看您的 sql 服务器使用的 .net 版本:
I just encountered exactly the same problem.
This is an old page, but first, formost and finally, the DLL must be built with .NET 2.0. SqlServer 2005 is built with .net 2.0, because that is the latest available when it was written. SqlServer 2008 might also have the same issue.
The DLL and the SqlServer must both reference the same .NET framekwork version. Once I figured this out, I created a separate project under 2.0 and shazam! worked perfect first time.
To see what version of .net your sql server uses:
接受的答案虽然似乎解决了OP的问题,但仅部分正确,并且对根本原因的解释过于简单,这可能会导致其他有类似问题的人走向错误的方向。
已接受答案的问题是对 .NET 环境的误解,同样的误解也可以在问题本身中看到。在 .NET 中,CLR 和框架是两个独立的东西,每个都有自己的版本。
CLR(公共语言运行时)是执行托管代码的。它的更新频率不如框架。 .NET Framework 是库的集合,提供与特定版本的 CLR 交互的基本方法。
单个版本的 CLR 通常具有与其配合使用的框架的多个版本。然而,单一版本的框架只能与一个特定版本的 CLR 配合使用。例如,CLR 版本 2.0 可与 Framework 版本 2.0、3.0 和 3.5 配合使用,而 CLR 版本 4.0 可与 .NET Framework 的所有 4.x 版本(即 4.0、4.5、4.5.1、4.5.2、4.6)配合使用。 , ETC)。要查看 CLR 版本与 Framework 版本关系的图表,请参阅 MSDN 页面.NET Framework 版本和依赖项< /a>.
对于SQLCLR代码,SQL Server仅适用于单一版本的CLR,具体版本取决于SQL Server的版本。 SQL Server 2005、2008 和 2008 R2 仅适用于 CLR 版本 2。由于 CLR 版本 2 仅适用于 .NET Framework 版本 2.0、3.0 和 3.5,这意味着 SQL Server 2005、2008 和 2008 R2 仅适用于 . NET Framework 版本 2.0、3.0 和 3.5。当然,SQL Server 2005 仅包含 .NET Framework 版本 2.0,因此 .NET Framework 版本 3.0 和 3.5 中有一些较新的库,如果不手动导入它们,这些库将无法在 SQL Server 2005 中运行(即 System.Core 和System.Xml.Linq)。同样,SQL Server 2012、2014 和 2016 静态链接到 CLR 版本 4,该版本可与 .NET Framework 版本 4.0、4.5、4.5.1、4.5.2、4.6 配合使用。
关于从
System.Environment.Version
(在问题中)和sys.dm_clr_properties.version
(在接受的答案中)返回的信息,他们报告了 < strong>CLR 版本,不是Framework 版本。因此,请注意不要将报告 2.0 或 4.0 的这两个内容混淆,这意味着您只能使用 Framework 版本 2.0 或 4.0。,由于向后兼容性,针对 CLR 2 Framework 版本(2.0、3.0 和 3.5)编译的代码无需在 SQL Server 2012 及更高版本中重新编译即可运行,即使它们使用的是 CLR 版本 4。
幸运的是 使用 2.0 版本的目标框架通常不会出错,但您肯定可以使用 2.0 以上的框架版本。
要更深入地了解开发 SQLCLR 代码,请查看我撰写的以下文章(以及整个系列):
SQLCLR 5 级阶梯:开发(在 SQL Server 中使用 .NET)
The accepted answer, while seeming to resolve the O.P.'s issue, is only partially correct and presents an overly simplistic explanation of the underlying cause, which could lead other people with a similar problem in the wrong direction.
The problem with the accepted answer is a misunderstanding of the .NET environment, and that same misunderstanding can also be seen in the Question itself. Within .NET, the CLR and the Framework are two separate things, each with their own versions.
The CLR (Common Language Runtime) is what executes managed code. This is not updated nearly as often as the Framework. The .NET Framework is a collection of libraries that provide the basic means of interacting with a particular version of the CLR.
A single version of the CLR typically has multiple versions of the Framework that it works with. A single version of the Framework, however, only works with one specific version of the CLR. For example, CLR version 2.0 works with Framework version 2.0, 3.0, and 3.5, while CLR version 4.0 works with all of the 4.x versions of the .NET Framework (i.e. 4.0, 4.5, 4.5.1, 4.5.2, 4.6, etc). To see the chart of CLR verion to Framework version relationships, see the MSDN page for .NET Framework Versions and Dependencies.
With regards to SQLCLR code, SQL Server only works with a single version of the CLR, and the specific version depends upon the version of SQL Server. SQL Server 2005, 2008, and 2008 R2 work only with CLR version 2. Since CLR version 2 only works with .NET Framework versions 2.0, 3.0, and 3.5, this means that SQL Server 2005, 2008, and 2008 R2 only work with .NET Framework versions 2.0, 3.0, and 3.5. Of course, SQL Server 2005 only included .NET Framework version 2.0 so there are a couple of newer libraries in .NET Framework version 3.0 and 3.5 that don't work in SQL Server 2005 without manually importing them (i.e. System.Core and System.Xml.Linq). Along those same lines, SQL Server 2012, 2014, and 2016 are statically linked to CLR version 4, which works with .NET Framework versions 4.0, 4.5, 4.5.1, 4.5.2, 4.6.
With regards to the information returned from both
System.Environment.Version
(in the Question) andsys.dm_clr_properties.version
(in the accepted answer), they are reporting the CLR version, not the Framework version. So be careful not to confuse those two things reporting 2.0 or 4.0 as meaning you can only use Framework version 2.0 or 4.0.And fortunately, due to backwards compatibility, code compiled against the CLR 2 Framework versions (2.0, 3.0, and 3.5) will run without needing to be recompiled in SQL Server 2012 and newer, even though they are on CLR version 4.
So, you generally cannot go wrong with using a Target Framework Version of 2.0, but you most certainly can use Framework versions beyond 2.0.
For a more in-depth look at developing SQLCLR code, check out the following article (and the series in general), which I wrote:
Stairway to SQLCLR Level 5: Development (Using .NET within SQL Server)
尝试一下,让我知道是否有效。
Try that, and let me know if that works.
CLR 不支持 System.Environment 库:
http://msdn.microsoft.com/en-us/library/ms403279.aspx
您仍然可以使用它,如上面文章的“不受支持的库”部分所示,但请记住,该代码尚未经过安全性和可靠性测试。这可能会在生产环境中导致不可预测的结果,因此在部署之前请考虑风险并仔细测试。
另外,我相信它要么必须有一个强名称,要么必须部署到标记为“可信”的数据库才能执行。
The library, System.Environment, is not supported for CLR:
http://msdn.microsoft.com/en-us/library/ms403279.aspx
You can still use it, as indicated in the "Unsupported Libraries" section of the article above, but keep in mind that the code has not been tested for security and reliability. This can cause unpredictable results in a production environment, so think about the risks and carefully test before deploying.
Also, I believe it either has to have a strong name or be deployed to a database marked as "Trustworthy" before it will execute.
简短回答:
在项目属性上设置 Sql Server 版本和 .Net Framework 版本。
首先,您必须检查设置您的项目属性。在项目属性中设置要为其创建 CLR 的 SQL Server 版本。然后选择.Net Framework版本。例如,如果您想为 SQL Server 2008 创建 CLR,则必须将 .Net Framework 设置为 3.5,对于 2005 选择 .Net 2.0。
我希望这个解决方案对您有所帮助。
Short answer :
set Sql Server version and .Net Framework version on project property.
First of all you have to check set your project property. in project property set the version of sql server that you want to create CLR for it. then choose .Net Framework version . for example if you want to create CLR for SQL Server 2008 you have to set .Net Framework to 3.5 and for 2005 choose .Net 2.0.
i hope this solution help you.