如何在SQL CLR项目中添加c#类库/类dll?

时间:2022-10-30 13:15:42

How do I add c# class library/ class dll in SQL CLR project ?.

如何在SQL CLR项目中添加c#类库/类dll?

I add the class library dll/ class dll but got error in SQL server 2012 during assembly creation

我添加了类库dll / class dll但在程序集创建期间在SQL Server 2012中出错

Assembly references assembly 'system.runtime.serialization, version=4.0.0.0, culture=neutral, 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.runtime.serialization,version = 4.0.0.0,culture = neutral,这在当前数据库中不存在。 SQL Server尝试从引用程序集来自的相同位置定位并自动加载引用的程序集,但该操作失败(原因:2(系统找不到指定的文件。))。请将引用的程序集加载到当前数据库中,然后重试您的请求。

1 个解决方案

#1


You have to register unsupported libraries before you can use them.

您必须先注册不受支持的库,然后才能使用它们。

-- You will have to use the Runtime Serialization from .NET v3

ALTER DATABASE [<<Database Name>>] SET TRUSTWORTHY ON;
CREATE ASSEMBLY AnyName_You_Want
FROM 
--'C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Runtime.Serialization.dll'
'C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\System.Runtime.Serialization.dll'
WITH PERMISSION_SET = UNSAFE;

Excerpt:

Unsupported libraries can still be called from your managed stored procedures, triggers, user-defined functions, user-defined types, and user-defined aggregates. The unsupported library must first be registered in the SQL Server database, using the CREATE ASSEMBLY statement, before it can be used in your code. Any unsupported library that is registered and run on the server should be reviewed and tested for security and reliability.

仍然可以从托管存储过程,触发器,用户定义函数,用户定义类型和用户定义聚合中调用不受支持的库。必须首先使用CREATE ASSEMBLY语句在SQL Server数据库中注册不受支持的库,然后才能在代码中使用它。应检查并测试在服务器上注册和运行的任何不受支持的库的安全性和可靠性。

For example, the System.DirectoryServices namespace is not supported. You must register the System.DirectoryServices.dll assembly with UNSAFE permissions before you can call it from your code. The UNSAFE permission is necessary because classes in the System.DirectoryServices namespace do not meet the requirements for SAFE or EXTERNAL_ACCESS. For more information, see CLR Integration Programming Model Restrictions and CLR Integration Code Access Security.

例如,不支持System.DirectoryServices命名空间。您必须先注册具有UNSAFE权限的System.DirectoryServices.dll程序集,然后才能从代码中调用它。 UNSAFE权限是必需的,因为System.DirectoryServices命名空间中的类不满足SAFE或EXTERNAL_ACCESS的要求。有关更多信息,请参阅CLR集成编程模型限制和CLR集成代码访问安全性。

CLR integration in SQL Server only supports a subset of .NET Framework Libraries. The libraries/namespaces supported by CLR integration in SQL Server are:

SQL Server中的CLR集成仅支持.NET Framework库的子集。 SQL Server中CLR集成支持的库/命名空间是:

  • CustomMarshalers
  • Microsoft.VisualBasic
  • Microsoft.VisualC
  • mscorlib
  • System
  • System.Configuration
  • System.Data
  • System.Data.OracleClient
  • System.Data.SqlXml
  • System.Deployment
  • System.Security
  • System.Transactions
  • System.Web.Services
  • System.Xml
  • System.Core.dll
  • System.Xml.Linq.dll

#1


You have to register unsupported libraries before you can use them.

您必须先注册不受支持的库,然后才能使用它们。

-- You will have to use the Runtime Serialization from .NET v3

ALTER DATABASE [<<Database Name>>] SET TRUSTWORTHY ON;
CREATE ASSEMBLY AnyName_You_Want
FROM 
--'C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Runtime.Serialization.dll'
'C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\System.Runtime.Serialization.dll'
WITH PERMISSION_SET = UNSAFE;

Excerpt:

Unsupported libraries can still be called from your managed stored procedures, triggers, user-defined functions, user-defined types, and user-defined aggregates. The unsupported library must first be registered in the SQL Server database, using the CREATE ASSEMBLY statement, before it can be used in your code. Any unsupported library that is registered and run on the server should be reviewed and tested for security and reliability.

仍然可以从托管存储过程,触发器,用户定义函数,用户定义类型和用户定义聚合中调用不受支持的库。必须首先使用CREATE ASSEMBLY语句在SQL Server数据库中注册不受支持的库,然后才能在代码中使用它。应检查并测试在服务器上注册和运行的任何不受支持的库的安全性和可靠性。

For example, the System.DirectoryServices namespace is not supported. You must register the System.DirectoryServices.dll assembly with UNSAFE permissions before you can call it from your code. The UNSAFE permission is necessary because classes in the System.DirectoryServices namespace do not meet the requirements for SAFE or EXTERNAL_ACCESS. For more information, see CLR Integration Programming Model Restrictions and CLR Integration Code Access Security.

例如,不支持System.DirectoryServices命名空间。您必须先注册具有UNSAFE权限的System.DirectoryServices.dll程序集,然后才能从代码中调用它。 UNSAFE权限是必需的,因为System.DirectoryServices命名空间中的类不满足SAFE或EXTERNAL_ACCESS的要求。有关更多信息,请参阅CLR集成编程模型限制和CLR集成代码访问安全性。

CLR integration in SQL Server only supports a subset of .NET Framework Libraries. The libraries/namespaces supported by CLR integration in SQL Server are:

SQL Server中的CLR集成仅支持.NET Framework库的子集。 SQL Server中CLR集成支持的库/命名空间是:

  • CustomMarshalers
  • Microsoft.VisualBasic
  • Microsoft.VisualC
  • mscorlib
  • System
  • System.Configuration
  • System.Data
  • System.Data.OracleClient
  • System.Data.SqlXml
  • System.Deployment
  • System.Security
  • System.Transactions
  • System.Web.Services
  • System.Xml
  • System.Core.dll
  • System.Xml.Linq.dll