SQL Server无法在我的DLL / Assembly中找到我的函数

时间:2017-02-17 16:15:14

标签: c# .net sql-server .net-assembly sqlclr

我使用此代码在C#中创建了一个库类,如您所见:

namespace ManagedCodeAndSQLServer
{
    public class BaseFunctionClass
    {
        public BaseFunctionClass()
        {
        }


        [SqlProcedure]
        public static void GetMessage(SqlString strName, out SqlString
        strMessge)
        {
            strMessge = "Welcome," + strName + ", " + "your code is getting executed under CLR !";

        }
    }
}

我使用UNSAFE Permission Set属性构建了这个项目,并使用以下代码将DLL添加到SQL Server:

use master;
grant external access assembly to [sa];
use SampleCLR;
CREATE ASSEMBLY ManagedCodeAndSQLServer
AUTHORIZATION dbo
FROM 'd:\ManagedCodeAndSQLServer.dll'
WITH PERMISSION_SET = UNSAFE
GO

它将程序集添加为我的数据库的一部分。

我想调用这个函数,你可以看到:

CREATE PROCEDURE usp_UseHelloDotNetAssembly
@name nvarchar(200),
@msg nvarchar(MAX)OUTPUT
AS EXTERNAL NAME ManagedCodeAndSQLServer.[ManagedCodeAndSQLServer.
BaseFunctionClass].GetMessage
GO

但是我收到了这个错误:

  

Msg 6505,Level 16,State 2,Procedure usp_UseHelloDotNetAssembly,Line 1   找不到Type'ManagedCodeAndSQLServer   程序集“ManagedCodeAndSQLServer”中的BaseFunctionClass'。

1 个答案:

答案 0 :(得分:2)

问题很微妙,而且在这种在线格式中,它甚至看起来只是格式化问题。但问题完全在这里,在方括号内:

AS EXTERNAL NAME ManagedCodeAndSQLServer.[ManagedCodeAndSQLServer.
BaseFunctionClass].GetMessage

ManagedCodeAndSQLServer.之后有一个不应该存在的实际换行符。它甚至反映在错误消息中:

  

Msg 6505,Level 16,State 2,Procedure usp_UseHelloDotNetAssembly,Line 1   找不到Type'ManagedCodeAndSQLServer   程序集“ManagedCodeAndSQLServer”中的BaseFunctionClass'。

这看起来像是一个单词问题 包装;-),但不是。如果删除换行符,则T-SQL显示如下:

AS EXTERNAL NAME ManagedCodeAndSQLServer.[ManagedCodeAndSQLServer.BaseFunctionClass].GetMessage;

然后它会正常工作。

一些补充说明:

  1. 无需授予sa任何内容。 sysadmin固定服务器角色的任何成员都已拥有所有权限。
  2. 除了SAFE之外,问题中显示的代码不需要标记为任何内容。除非绝对必要,否则请勿将任何大会标记为UNSAFE
  3. 无需将数据库设置为TRUSTWORTHY ON。这是一种安全风险,应该避免,除非绝对必要
  4. 如果您正在学习SQLCLR,请参阅我在SQL Server Central上就该主题撰写的系列文章:Stairway to SQLCLR(需要免费注册才能阅读其内容,但值得: - )。
相关问题