C# 将UDF与SQL查询一起使用时出错
我已经使用SQL项目模板在Visual studio中创建了一个CLR,使用C语言作为:C# 将UDF与SQL查询一起使用时出错,c#,sql,sql-server,sqlclr,C#,Sql,Sql Server,Sqlclr,我已经使用SQL项目模板在Visual studio中创建了一个CLR,使用C语言作为: [Microsoft.SqlServer.Server.SqlFunction()] public static SqlDateTime ScalarUDF(SqlInt64 CompanyID) { SqlInt64 temp = CompanyID; string zoneId = "Singapore Standard Time";
[Microsoft.SqlServer.Server.SqlFunction()]
public static SqlDateTime ScalarUDF(SqlInt64 CompanyID)
{
SqlInt64 temp = CompanyID;
string zoneId = "Singapore Standard Time";
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(zoneId);
DateTime result = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi);
return new SqlDateTime(result);
}
并且还使用query:将UDF创建到SQL中:
-- Install Assembly
CREATE ASSEMBLY UDF_Trial FROM 'C:\Users\Rahul\Documents\visual studio 2013\Projects\UDF_Trial\UDF_Trial\bin\Debug\UDF_Trial.dll'
GO
-- Create ScalarUDF
CREATE FUNCTION [dbo].[ScalarUDF](@CompanyID bigint)
RETURNS datetime
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME UDF_Trial.UserDefinedFunctions.ScalarUDF;
GO
但在执行UDF时,我遇到了一些错误:
我执行UDF作为
选择[dbo].[ScalarUDF]15
但将异常获取为::
Msg 6522, Level 16, State 2, Line 2
A .NET Framework error occurred during execution of user-defined routine or aggregate "ScalarUDF":
System.Security.HostProtectionException: Attempted to perform an operation that was forbidden by the CLR host.
The protected resources (only available with full trust) were: All
The demanded resources were: MayLeakOnAbort
System.Security.HostProtectionException:
at UserDefinedFunctions.ScalarUDF(SqlInt64 CompanyID)
.
我还将创建程序集时的不安全权限设置为::
从“C:\Users\Rahul\Desktop\pro\u temp\UDF\u Trial.dll”创建程序集UDF\u试用版
权限设置为不安全;
去
但在这种情况下,我得到的错误如下:
CREATE ASSEMBLY for assembly 'UDF_Trial' failed because assembly 'UDF_Trial' is not authorized for PERMISSION_SET = UNSAFE. The assembly is authorized when either of the following is true: the database owner (DBO) has UNSAFE ASSEMBLY permission and the database has the TRUSTWORTHY database property on; or the assembly is signed with a certificate or an asymmetric key that has a corresponding login with UNSAFE ASSEMBLY permission.
我已将数据库的Trustly设置为ON::
ALTER DATABASE Learn SET TRUSTWORTHY ON
并已将程序集创建为::
CREATE ASSEMBLY UDF_Blog FROM 'C:\Users\Rahul\Documents\visual studio 2013\Projects\UDF_Trial\UDF_Trial\bin\Debug\UDF_Trial.dll'
WITH PERMISSION_SET = UNSAFE;
GO
问题解决了。我已将数据库的Trustly设置为ON::
ALTER DATABASE Learn SET TRUSTWORTHY ON
并已将程序集创建为::
CREATE ASSEMBLY UDF_Blog FROM 'C:\Users\Rahul\Documents\visual studio 2013\Projects\UDF_Trial\UDF_Trial\bin\Debug\UDF_Trial.dll'
WITH PERMISSION_SET = UNSAFE;
GO
问题解决了。我已将数据库的Trustly设置为ON::
ALTER DATABASE Learn SET TRUSTWORTHY ON
并已将程序集创建为::
CREATE ASSEMBLY UDF_Blog FROM 'C:\Users\Rahul\Documents\visual studio 2013\Projects\UDF_Trial\UDF_Trial\bin\Debug\UDF_Trial.dll'
WITH PERMISSION_SET = UNSAFE;
GO
问题解决了。我已将数据库的Trustly设置为ON::
ALTER DATABASE Learn SET TRUSTWORTHY ON
并已将程序集创建为::
CREATE ASSEMBLY UDF_Blog FROM 'C:\Users\Rahul\Documents\visual studio 2013\Projects\UDF_Trial\UDF_Trial\bin\Debug\UDF_Trial.dll'
WITH PERMISSION_SET = UNSAFE;
GO
问题解决了。可能重复@MoslemBenDhaou给出正确的答案,而不是查看其他人的答案。在提问之前,请阅读StackOverflow指南。可能重复@MoslemBenDhaou给出正确的答案,而不是查看其他人的答案。提问前请阅读StackOverflow指南。可能重复@MoslemBenDhaou给出正确的答案,而不是查看其他人的答案。提问前请阅读StackOverflow指南。可能重复@MoslemBenDhaou给出正确的答案,而不是查看其他人的答案。提问前请阅读StackOverflow指南。