SQL CLR Issue - TimeZoneInfo Class

  • Hello All,

    I have created SQL CLR Function for Timezone conversion and successfully deployed. here is my code,

    public partial class UserDefinedFunctions

    {

    [Microsoft.SqlServer.Server.SqlFunction]

    public static SqlDateTime TimezoneConversion(SqlDateTime _date, SqlString _currentTZ, SqlString _convertingTZ)

    {

    SqlDateTime _convertedDate = new SqlDateTime();

    TimeZoneInfo _currentZone = TimeZoneInfo.FindSystemTimeZoneById(_currentTZ.Value);

    TimeZoneInfo _convertingZone = TimeZoneInfo.FindSystemTimeZoneById(_convertingTZ.Value);

    _convertedDate = TimeZoneInfo.ConvertTime(_date.Value, _currentZone, _convertingZone);

    return _convertedDate;

    }

    };

    the assembly is Dot.net 3.5 Framework and Database is SQL Server 2008, i deployed with SAFE Permission Level. When I tried to do with External or Unsafe Mode, my visual studio 2010 shows the deployment is failed, i couldn't know much about it, because it doesn't any other message, just shows deployment failed.

    I tried to execute the function, but it throws an error as below,

    Msg 6522, Level 16, State 2, Line 1

    A .NET Framework error occurred during execution of user-defined routine or aggregate "TimezoneConversion":

    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.TimezoneConversion(SqlDateTime _date, SqlString _currentTZ, SqlString _convertingTZ)

    please some one help me to resolve the issue.

    Thanks in advance.

    Raja.

    Regards,
    Ilayaraja Chandrasekaran
    Sr. Software Engineer,
    Softura Private Limited, Chennai.
    India - 600014.

  • the key is to look in the Output window for the issue:

    i built a new project with your code, changed it to External and tried to deploy.

    i had the wrong default run time the first pass.(4.0).

    once i changed it to 3.5, it worked for me (on a trustworthy database)

    what is the contents of your output window after the error?

    this is the results of the output window:

    ------ Build started: Project: SqlServerProject1, Configuration: Debug Any CPU ------

    SqlServerProject1 -> C:\_PA\Workspace\All_Workspaces\Workspace_CLR\SqlServerProject1\SqlServerProject1\bin\Debug\SqlServerProject1.dll

    ------ Deploy started: Project: SqlServerProject1, Configuration: Debug Any CPU ------

    Build started 6/1/2012 9:39:22 AM.

    SqlClrDeploy:

    Beginning deployment of assembly SqlServerProject1.dll to server DEV223 : SandBox

    The following error might appear if you deploy a SQL CLR project that was built for a version of the .NET Framework that is incompatible with the target instance of SQL Server: "Deploy error SQL01268: CREATE ASSEMBLY for assembly failed because assembly failed verification". To resolve this issue, open the properties for the project, and change the .NET Framework version.

    Deployment script generated to:

    C:\_PA\Workspace\All_Workspaces\Workspace_CLR\SqlServerProject1\SqlServerProject1\bin\Debug\SqlServerProject1.sql

    Creating [SqlServerProject1]...

    C:\_PA\Workspace\All_Workspaces\Workspace_CLR\SqlServerProject1\SqlServerProject1\bin\Debug\SqlServerProject1.sql(37-37): Deploy error SQL01268: .Net SqlClient Data Provider: Msg 6257, Level 16, State 1, Line 1 CREATE ASSEMBLY for assembly 'SqlServerProject1' failed because the assembly is built for an unsupported version of the CLR runtime.

    An error occurred while the batch was being executed.

    Build FAILED.

    Time Elapsed 00:00:10.82

    ========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

    ========== Deploy: 0 succeeded, 1 failed, 0 skipped ==========

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks Lowell...

    i set TROUSTWORTHY ON and now it works....:-)

    Regards,
    Ilayaraja Chandrasekaran
    Sr. Software Engineer,
    Softura Private Limited, Chennai.
    India - 600014.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply