我正在尝试为使用.net 3.5程序集的sql 2005数据库创建一个clr存储过程
所以首先我必须更改sql 2005来识别system.core是不安全的,我不太高兴(我宁愿让它说SAFE)。
现在我收到此错误
Msg 6522, Level 16, State 1, Procedure StoredProcedure1, Line 0
A .NET Framework error occurred during execution of user defined routine or aggregate 'StoredProcedure1':
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 StoredProcedures.StoredProcedure1(String UtcDateTime)
这是我的代码
Exec StoredProcedure1 '7/8/2010 5:00:00 am'
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void StoredProcedure1(string UtcDateTime)
{
SqlPipe p = SqlContext.Pipe;
DateTime converted = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(Convert.ToDateTime(UtcDateTime), "Pacific Standard Time");
p.Send(converted.ToString());
}
};
我不知道如何将日期时间传入其中,所以我使用了一个字符串然后转换它。
答案 0 :(得分:6)
要使其工作,您必须将其设置为UNSAFE。
显然,某些TimeZoneInfo方法设置了HostProtectionAttribute,这意味着它们不能在SQL Server CLR代码中使用。
除非你决定“我不关心稳定性并且知道更好”。如果您使用UNSAFE,如果您的服务器成为地面上的吸烟坑,我不承担任何责任......