Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

c# - How to make this CLR work with 2005?

I am trying to make a clr stored procedure for a sql 2005 database that uses .net 3.5 assemblies

So first I had to change sql 2005 to recognize system.core as unsafe what I am not too happy about(I rather have had it say SAFE).

Now I get this error

 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)

Here is my code

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

    }
};

I did not know how to pass in a datetime into it so I used a string then converted it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To get this to work you'd have to set it as UNSAFE.

Apparently, some TimeZoneInfo methods have HostProtectionAttribute set which means they can't be used in SQL Server CLR code.

Unless you decide "I don't care about stability and know better". I take no responsibility if your server becomes a smoking crater in the ground if you use UNSAFE...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...