我使用此代码在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'。
答案 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;
然后它会正常工作。
一些补充说明:
sa
任何内容。 sysadmin
固定服务器角色的任何成员都已拥有所有权限。SAFE
之外,问题中显示的代码不需要标记为任何内容。除非绝对必要,否则请勿将任何大会标记为UNSAFE
。TRUSTWORTHY ON
。这是一种安全风险,应该避免,除非绝对必要。