2

我正在尝试为使用 .net 3.5 程序集的 sql 2005 数据库创建一个 clr 存储过程

因此,首先我必须更改 sql 2005 以将 system.core 识别为不安全的,这是我不太满意的(我宁愿让它说安全)。

现在我得到这个错误

 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());

    }
};

我不知道如何将日期时间传递给它,所以我使用了一个字符串然后对其进行了转换。

4

1 回答 1

6

要使其正常工作,您必须将其设置为不安全。

显然,某些 TimeZoneInfo 方法设置了HostProtectionAttribute,这意味着它们不能在 SQL Server CLR 代码中使用。

除非您决定“我不关心稳定性并且知道得更好”。如果您使用 UNSAFE,如果您的服务器变成地下冒烟的火山口,我不承担任何责任...

于 2010-07-09T06:24:09.933 回答