SQL Server:在程序集中找不到类型
假设程序集 dll:
using Microsoft.SqlServer.Server;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System;
using System.Text;
namespace CLRFunctions
{
public class T
{
[SqlFunction(DataAccess = DataAccessKind.Read)]
public static String NormalizeString(String s, String normalizationForm)
{
NormalizationForm form = NormalizationForm.FormC;
if (String.Equals(f, "FormD", StringComparison.OrdinalIgnoreCase))
form = NormalizationForm.FormD;
return s.Normalize(form);
}
}
}
注意:将程序集定位到 .NET 3.5,因为 SQL Server 不支持 .NET 4.0
将程序集复制到某个位置,并且“创建”程序集工作正常:
CREATE ASSEMBLY CLRFunctions FROM 'c:\Program Files\My App\CLRFunctions.dll';
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'clr enabled', 1;
GO
RECONFIGURE;
GO
创建用户定义函数失败:
CREATE FUNCTION NormalizeString(@s nvarchar(max), @normalizationForm varchar(50))
RETURNS nvarchar(max)
AS EXTERNAL NAME CLRFunctions.T.NormalizeString
失败错误:
Msg 6505, Level 16, State 2, Procedure NormalizeString, Line 1
Could not find Type 'T' in assembly 'CLRFunctions'.
为什么 SQL Server 在程序集 CLRFunctions
中找不到类型 T
?
注意:为什么是 T
? 因为微软做到了。
Assume the assembly dll:
using Microsoft.SqlServer.Server;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System;
using System.Text;
namespace CLRFunctions
{
public class T
{
[SqlFunction(DataAccess = DataAccessKind.Read)]
public static String NormalizeString(String s, String normalizationForm)
{
NormalizationForm form = NormalizationForm.FormC;
if (String.Equals(f, "FormD", StringComparison.OrdinalIgnoreCase))
form = NormalizationForm.FormD;
return s.Normalize(form);
}
}
}
Note: Target the assembly to .NET 3.5 as SQL Server doesn't support .NET 4.0
Copy the assembly to a location, and "creating" the assembly works fine:
CREATE ASSEMBLY CLRFunctions FROM 'c:\Program Files\My App\CLRFunctions.dll';
Note: And then enable CLR functions, otherwise they are broken by default:
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'clr enabled', 1;
GO
RECONFIGURE;
GO
Created the user-defined function fails:
CREATE FUNCTION NormalizeString(@s nvarchar(max), @normalizationForm varchar(50))
RETURNS nvarchar(max)
AS EXTERNAL NAME CLRFunctions.T.NormalizeString
fails with error:
Msg 6505, Level 16, State 2, Procedure NormalizeString, Line 1
Could not find Type 'T' in assembly 'CLRFunctions'.
Why can SQL Server not find type T
in assembly CLRFunctions
?
Note: Why T
? Cause Microsoft did.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
Try
我刚刚在 Visual Studio 2017 中绞尽脑汁,用 VB 构建了一个 CLR。
我发现,在 SQL 中创建过程时,外部名称设置如下:
AssemblyName
.[Assemblyname.ClassNameInVBProgram]
.SubroutineNameInVBProgram
并且它区分大小写。
在 SQL 中使用
Create Assembly
创建 Sql 程序集在 SQL 中使用
Create procedure
创建 CLR SP。I just busted my skull on this in Visual Studio 2017, building a CLR in VB.
What I found out, when creating the procedure in SQL, THE EXTERNAL NAME is set as follows:
AssemblyName
.[Assemblyname.ClassNameInVBProgram]
.SubroutineNameInVBProgram
And it is Case Sensitive.
Use
Create Assembly
in SQL to create the Sql AssemblyUse
Create Procedure
in SQL to create the CLR SP.