SQL Server 2017 CLR Error: Could Not Find type in assembly

SQL Server 2017 CLR Error: Could Not Find type in assembly

我正在 SQL Server 2017 中创建一个 CLR 存储过程。我在以前的版本中已经多次这样做,没有任何问题;但是,我们最近更新到 SQL Server 2017。

我知道此升级带来的安全问题,但我想确保在开始搞乱安全性之前我可以部署我的 CLR。我创建了一个名为 Test 的数据库并将 Trustworthy 设置为 ON(我知道不好——临时的)。我不断收到以下错误:

Could not find Type 'CLR_Get_API_Data.StoredProcedures' in assembly 'Get_API_Data'.

我找到了几篇文章,例如:SQL Server: Could not find type in the assembly 处理此错误,但他们在 class 中构建了代码,而不是 storedProcedures 类型的部分 class .

这是我的 SQL 创建存储过程的脚本:

USE Test 
GO

--Alter Database Test
--Set Trustworthy on

--EXEC sp_configure 'clr strict security', 1;
--RECONFIGURE;

IF EXISTS (SELECT * FROM sys.assemblies asms 
           WHERE asms.name = N'Get_API_Data' AND is_user_defined = 1)
    DROP ASSEMBLY [Get_API_Data]
GO

CREATE ASSEMBLY Get_API_Data
FROM 'C:\Users\Administrator\Documents\Visual Studio 2015\Projects\CLR_Assemblies\Get_API_Data\Get_API_Data\bin\Debug\Get_API_Data.dll'
WITH Permission_Set = Safe --EXTERNAL_ACCESS 
GO

--Assembly Name,[SolutionName.StoredProcedures].Sub Name
CREATE PROCEDURE [dbo].[GetAPI]
AS EXTERNAL NAME Get_API_Data.[CLR_Get_AMS_API_Data.StoredProcedures].GetAPI
GO

我的解决方案名称是 CLR_Get_API_Data,我的项目名称是 Get_API_Data,我的方法名称是 GetAPI。以下是我的 Visual Studio 代码:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Net;
using System.IO;

public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void GetAPI ()
    {
         // Stuff happens here
    }    //Ends Public Void Get
}    //Ends Class

使用部分 class 没问题。

[CLR_Get_AMS_API_Data.StoredProcedures]CLR_Get_AMS_API_Data 部分引用命名空间名称。您的代码未显示您正在使用命名空间。删除 CLR_Get_AMS_API_Data. 这样您就剩下:

AS EXTERNAL NAME Get_API_Data.[StoredProcedures].GetAPI

有关在 SQL Server 2017 及更高版本中正确/安全部署 SQLCLR 项目的更多信息,请参阅我的帖子:

  1. SQLCLR vs. SQL Server 2017, Part 2: “CLR strict security” – Solution 1 — more steps than Part 3, Solution 2 (below), but a good fit for existing projects as it requires almost no changes to the existing solution or even deployment process (and in fact, this is effectively the route that I went for my SQL# 项目只是在安装脚本的开头添加了 3 个简单的步骤)
  2. SQLCLR vs. SQL Server 2017, Part 3: “CLR strict security” – Solution 2

有关使用 SQLCLR 的更多信息,请访问:SQLCLR Info