all groups > sql server programming > november 2006 >
You're in the

sql server programming

group:

using SQLCLR VB.NET


using SQLCLR VB.NET phil
11/12/2006 8:19:01 PM
sql server programming:
I've just started to integrate a VB.net assembly into an SQL-SERVER database
I've brought in the assembly and created an SQL procedure which finds its
way to the VB code
It all worked fine while I was just handling simple commands to test it out
but now I've added some database accessing and I get errors

If anyone has any ideas about how to solve this I'd be very grateful
Phil

---------------------------------------------------------------------------------------
my VB code followed by the exception raised when I try to run this from
SQL-SERVER
....
Partial Public Class EOL
<Microsoft.SqlServer.Server.SqlProcedure()> _
Public Shared Sub ProjectUpdate(ByVal projectID As Integer)
Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim rdr As SqlDataReader
Dim constr As String="Data Source=oakleafserver\sqlexpress;Initial
Catalog=estimatingOnLine;Integrated Security=True"
conn = New SqlConnection(constr)
conn.Open()
cmd = New SqlCommand("Select * from Projects_items", conn)
rdr = cmd.ExecuteReader()
SqlContext.Pipe.Send(rdr)
conn.Close()

SqlContext.Pipe.Send("Hello from VB ")

End Sub
....

Msg 6522, Level 16, State 1, Procedure ProjectUpdate, Line 0
A .NET Framework error occurred during execution of user defined routine or
aggregate 'ProjectUpdate':
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 estimatingOnLine.EOL.ProjectUpdate(Int32 projectID)

Re: using SQLCLR VB.NET Erland Sommarskog
11/12/2006 9:40:01 PM
phil (phil@oakleafsoftware.co.uk) writes:
[quoted text, click to view]

Are you trying to get back data from the same server the procedure is
running on? In this case, you should use the context connection, unless
you specifically want to create a loopback connection.

If you want to connect to a different server (or do a loopback), you are
getting outside the sandbox defined as "safe", and you must create the
assembly with the permission set EXTERNAL_ACCESS or UNSAFE. (I would
guess that EXTERNAL_ACCESS suffices in this case, but I don't know.)

You can read about the context connection in Books Online on this URL:
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/denet9/html/67dd1925-d672-4986-a85f-bce4fe832ef7.htm

And about permission sets here:
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/denet9/html/2111cfe0-d5e0-43b1-93c3-e994ac0e9729.htm

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: using SQLCLR VB.NET phil
11/12/2006 9:57:36 PM
worked a treat

many thanks for helping out - and so quickly too for Sunday evening

all the very best
Phil

[quoted text, click to view]

AddThis Social Bookmark Button