![](/img/trans.png)
[英]Can't find my c# CLR table value assembly in the list or create the associate SQL function
[英]SQL Server can't find my function in my DLL / Assembly
您可以使用以下代碼在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
但是我得到這個錯誤:
消息6505,級別16,狀態2,過程usp_UseHelloDotNetAssembly,第1行
找不到類型'ManagedCodeAndSQLServer。
程序集“ ManagedCodeAndSQLServer”中的“ BaseFunctionClass”。
問題很細微,在這里這樣的在線格式中,它甚至似乎只是格式問題。 但問題完全在方括號內找到:
AS EXTERNAL NAME ManagedCodeAndSQLServer.[ManagedCodeAndSQLServer.
BaseFunctionClass].GetMessage
ManagedCodeAndSQLServer.
之后有一個實際的換行符ManagedCodeAndSQLServer.
那不應該在那里。 它甚至反映在錯誤消息中:
消息6505,級別16,狀態2,過程usp_UseHelloDotNetAssembly,第1行
找不到類型'ManagedCodeAndSQLServer。
程序集“ ManagedCodeAndSQLServer”中的“ BaseFunctionClass”。
再次看起來像是個單詞問題
包裝;-),但不是。 如果刪除了換行符,則T-SQL如下所示:
AS EXTERNAL NAME ManagedCodeAndSQLServer.[ManagedCodeAndSQLServer.BaseFunctionClass].GetMessage;
那么它將正常工作。
一些附加說明:
sa
東西。 sysadmin
固定服務器角色的任何成員都已經具有所有權限。 SAFE
。 除非絕對必要,否則請勿將任何大會標記為UNSAFE
。 TRUSTWORTHY ON
。 這是安全隱患,除非絕對必要,否則應避免。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.