Я думаю, что это может быть то же самое, что и This Issue
Я также ссылался на него, но до сих пор не смог найти решение своей проблемы. Это первый раз, когда я создаю CLR и получаю проблему.
Я разработал CLR в С# как ::
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlDateTime GetTimeForCompanyID(SqlInt64 CompanyID)
{
string strZoneID;
string connStr = @"data source=.;initial catalog=dbName;Integrated Security=SSPI;user id=RJ;password=RJ@123;enlist=false;";
using (SqlConnection connection = new SqlConnection(connStr))
{
connection.Open();
using (SqlCommand select = new SqlCommand(
"select SettingValue from CompanySetting where CompanyID="+CompanyID+" and Setting='TimeZone'",
connection))
{
using (SqlDataReader reader = select.ExecuteReader())
{
strZoneID = reader.GetString(0);
}
}
}
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(strZoneID);
DateTime result = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi);
SqlDateTime Final = result;
return Final;
}
}
а создание сборки в SQL так::
1) Я создал Assembly Key
сначала в основной базе данных как ::
use master
CREATE ASYMMETRIC KEY MyDllKey FROM EXECUTABLE FILE = 'C:\Temp\CueBusinessFunctions.dll'
CREATE LOGIN MyDllLogin FROM ASYMMETRIC KEY MyDllKey
GRANT EXTERNAL ACCESS ASSEMBLY TO MyDllLogin
2) Создал сборку как::
use Learn
CREATE ASSEMBLY CueBusinessFunctions FROM 'C:\Projects\CueBusinessFunctions.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS
GO
3) Создание CLR UDF как::
CREATE FUNCTION [dbo].[GetTimeForCompanyID](@CompanyID bigint)
RETURNS datetime
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME CueBusinessFunctions.UserDefinedFunctions.GetTimeForCompanyID;
GO
Но проблема заключается в выполнении UDF как ::
select [dbo].[GetTimeForCompanyID](1)
Я получаю ошибку как::
Msg 6522, Level 16, State 1, Line 2
A .NET Framework error occurred during execution of user-defined routine or aggregate "GetTimeForCompanyID":
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.GetTimeForCompanyID(SqlInt64 CompanyID)
.