Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL CLR Issue - TimeZoneInfo Class Expand / Collapse
Author
Message
Posted Friday, June 1, 2012 5:58 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, May 26, 2014 10:59 PM
Points: 12, Visits: 61
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.
Post #1309683
Posted Friday, June 1, 2012 7:43 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:24 PM
Points: 12,906, Visits: 31,984
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1309728
Posted Friday, June 1, 2012 9:32 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, May 26, 2014 10:59 PM
Points: 12, Visits: 61
Thanks Lowell...

i set TROUSTWORTHY ON and now it works....



Regards,
Ilayaraja Chandrasekaran
Sr. Software Engineer,
Softura Private Limited, Chennai.
India - 600014.
Post #1309821
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse