SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL CLR Issue - TimeZoneInfo Class


SQL CLR Issue - TimeZoneInfo Class

Author
Message
ilayaraja84
ilayaraja84
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 66
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.
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28072 Visits: 39926
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!

ilayaraja84
ilayaraja84
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 66
Thanks Lowell...

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

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


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search