36

Receiving the following error when attempting to run a CLR stored proc. Any help is much appreciated.

Msg 10314, Level 16, State 11, Line 1
An error occurred in the Microsoft .NET Framework while trying to load assembly id 65752. The server may be running out of resources, or the assembly may not be trusted with PERMISSION_SET = EXTERNAL_ACCESS or UNSAFE. Run the query again, or check documentation to see how to solve the assembly trust issues. For more information about this error: 
System.IO.FileLoadException: Could not load file or assembly 'orders, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An error relating to security occurred. (Exception from HRESULT: 0x8013150A)
System.IO.FileLoadException: 
   at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
   at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
   at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   at System.Reflection.Assembly.Load(String assemblyString)
Solomon Rutzky
  • 46,688
  • 9
  • 128
  • 171
homeskillet
  • 1,169
  • 1
  • 10
  • 10

5 Answers5

69

Ran the SQL commands below and the issue appears to be resolved.

USE database_name
GO

EXEC sp_changedbowner 'sa'
ALTER DATABASE database_name SET TRUSTWORTHY ON 
Justin Dearing
  • 14,270
  • 22
  • 88
  • 161
homeskillet
  • 1,169
  • 1
  • 10
  • 10
  • 2
    The sp_changedbowner fixed it! More details on http://support.microsoft.com/kb/918040 – Gerardo Grignoli Aug 30 '11 at 18:23
  • 2
    @JustinDearing and others: Please do not set databases to `TRUSTWORTHY ON` unless there is no other choice (and there usually is). Please see my [answer](http://stackoverflow.com/a/32169987/577765) on this page for details. – Solomon Rutzky Aug 23 '15 at 18:28
  • As far as I can tell the user you want to use in `sp_changedbowner` should be the same user as the `msdb`, `temp` and other system tables are using. At least in most scenarios I've seen. You usually need to change this when you do a restore, since it will set the user running the restore as db owner. – Erk Feb 08 '17 at 14:59
8

Build your project with ANY CPU configuration. I had this problem when compiled my own project with x86 configuration and tried to run it on x64 SQL server.

nuwanda
  • 81
  • 1
  • 1
2

Applied all of the above suggestion and it failed. Then I recompiled my source code with "Any CPU" option, and it worked!

This link helped: SQL Server failed to load assembly with PERMISSION

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
101V
  • 492
  • 1
  • 6
  • 13
0

Does your assembly do file I/O? If so, you must grant the assembly permission to do this. In SSMS:

  1. Expand "Databases"
  2. Expand the node for your database
  3. Expand "Programmability"
  4. Expand "Assemblies"
  5. Right-click your assembly, choose Properties
  6. On the "General" page, change "Permission set" to "External access"
Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
-1
ALTER AUTHORIZATION ON DATABASE::mydb TO sa;
ALTER DATABASE [myDB] SET TRUSTWORTHY ON
GO
Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65