I got the sql procedure from the CLR (.net Assembly), which returns an error when executed
Msg 6522, Level 16, State 1, Procedure sp_HelloWorld, Line 0 A .NET Framework error occurred during execution of user defined routine or aggregate 'sp_HelloWorld': System.Security.SecurityException: Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. System.Security.SecurityException: at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) at System.Security.PermissionSet.Demand() at System.Data.Common.DbConnectionOptions.DemandPermission() at System.Data.SqlClient.SqlConnection.PermissionDemand() at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at HelloWorld.SQLCLR.HelloWorld()
This is my SQL script
go drop procedure HelloWorld drop assembly HelloWorld GO create assembly HelloWorld from 'F:\HelloWorld.dll' with permission_set = safe Go create procedure sp_HelloWorld as external name HelloWorld.[HelloWorld.SQLCLR].HelloWorld go exec sp_HelloWorld
and this is my class (assembly)
using Microsoft.SqlServer.Server; using System.Data.SqlTypes; using System.Data.SqlClient; using System.Security.Permissions; using System.Data; namespace HelloWorld { public class SQLCLR { [Microsoft.SqlServer.Server.SqlProcedure] public static void HelloWorld() { string connectString1 = @"Data Source=localhost;Initial Catalog=ItemData;Integrated Security=True"; SqlClientPermission permission = new SqlClientPermission(PermissionState.None); permission.Add(connectString1, "", KeyRestrictionBehavior.AllowOnly); permission.PermitOnly(); SqlConnection sqlcon = new SqlConnection(connectString1); sqlcon.Open(); SqlCommand sqlcmd = new SqlCommand("SELECT Top 1 * FROM ItemData.dbo.Item", sqlcon); SqlDataReader reader = sqlcmd.ExecuteReader(); SqlContext.Pipe.Send(reader); sqlcon.Close(); } } }
Juvil source share