winforms 尝试加载程序集ID 65675时,Microsoft .NET Framework中出错

k4ymrczo  于 2023-05-07  发布在  .NET
关注(0)|答案(6)|浏览(304)

我必须在现有的应用程序,包括许多项目,包括一个数据库项目。在statup项目中,这是一个windows应用程序,当调用Adapter.Fill(dataTable);时,会出现一个可怕的错误:

An error occurred in the Microsoft .NET Framework while trying to load assembly id 65675. 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 'xxxxx.yyyy.database, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
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.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)
 (.Net SqlClient Data Provider)


   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
   at XXX.YY.Data.dsLandholdingsTableAdapters.LandholdingsTableAdapter.Fill(LandholdingsDataTable dataTable, String IV_LRNO, String SearchField, String SearchVal, Nullable`1 SearchType) in E:\Projects\PPP\XXX\YYY\Data\dsLandholdings.Designer.cs:line 5678
   at XXX.YYY.Browse.BrowseLandholdings.btnSearch_Click(Object sender, EventArgs e) in E:\Projects\PPPP\XXXX\YYY\Browse\BrowseLandholdings.cs:line 352

在网上调查后,我发现将“数据库项目属性”的“数据库”选项卡上的“权限级别”从“安全”更改为“不安全”或“外部”可能会有所帮助。

但是在进行上述更改后,错误仍然出现!
有人知道为什么会这样吗?
任何帮助提前赞赏。

ars1skjm

ars1skjm1#

这对我来说是一个陷阱:

USE <DATABASE>;
EXEC sp_configure 'clr enabled' ,1
GO

RECONFIGURE
GO
EXEC sp_configure 'clr enabled'   -- make sure it took
GO

USE <DATABASE>
GO

EXEC sp_changedbowner 'sa'
USE <DATABASE>
GO

ALTER DATABASE <DATABASE> SET TRUSTWORTHY ON;
hfsqlsce

hfsqlsce2#

这对我很有效

EXEC sp_configure 'show advanced options', '1';
GO
RECONFIGURE;
GO

--Enable CLR (.NET Common Language Runtime)
exec sp_configure 'clr enabled', '1';
GO
RECONFIGURE;
GO
--Enable xp_CmdShell stored procedure to run Command Line programs from within T-SQL
--EXEC sp_configure 'xp_cmdshell', 1
--GO

EXEC sp_configure 'show advanced options', '0';
GO
RECONFIGURE;

 /****this is another one****/
USE master
GO
ALTER DATABASE <DB_NAME>SET TRUSTWORTHY ON

USE <DB_NAME>
GO
EXEC sp_changedbowner 'sa'
/****when error occured relating to m,emory***/
x6h2sr28

x6h2sr283#

最后,我终于能修好它了,
1.在SQL Server中启用“CLR Integration”。

  1. Deploy the CLR Object,在我的例子中是数据库项目,我还将权限级别设置为外部。
    感谢所有的评论/回答。
2ic8powd

2ic8powd4#

堆栈跟踪指向它无法加载的程序集。如果您使用.dll文件来构建或加载任何内容(数据导入/导出),您可能希望从那里开始。
检查以确保程序集是应用程序的正确版本,或者它甚至存在于目录中。
另外,请仔细检查程序集中的命名约定。堆栈跟踪显示程序集名称或代码库无效。该语言在应用程序中是否可解析?名字里有错别字吗?
这些可能是非常基本的开始,所以如果你已经尝试过这些,我很抱歉。我以前遇到过这样的跟踪,在我的情况下,它通常相当于缺少.dll或版本不兼容。

7kqas0il

7kqas0il5#

确保从中使用xxxxx.yyyy.database程序集的程序集不是针对旧版本的. NETFramework。

nvbavucw

nvbavucw6#

对于那些在SQL Server 16或更高版本上希望使用受信任程序集并保持clr strict安全启用的用户,以下是对我有效的步骤:
1.删除不工作的麻烦程序集(如果我没有先这样做,下面的步骤就不起作用)DROP ASSEMBLY [YourAssembly]
1.运行配置更改:

USE [YourDatabase]
EXEC sp_configure 'clr enabled', '1'
EXEC sp_configure 'show advanced options', '1'
EXEC sp_configure 'clr strict security', '1' --we want to use strict security
RECONFIGURE

1.添加程序集

USE [YourDatabase]
CREATE ASSEMBLY [YourAssembly] ...

在数据库所有者权限方面,您可能会在此处遇到错误。我们希望将主数据库的所有者设置为与目标数据库相同。

The database owner SID recorded in the master database differs from the database owner SID recorded in database.
USE [YourDatabase]
DECLARE @serverOwner VARCHAR(MAX)
SELECT serverOwner  = suser_sname( owner_sid ) FROM sys.databases WHERE name = 'master'
exec sp_changedbowner @serverOwner

然后重新运行创建程序集脚本。
1.添加受信任的程序集

USE [YourDatabase]
declare
     @hash binary(64)
    ,@description nvarchar(4000)

select
      @hash = HASHBYTES('SHA2_512', af.content)
    , @description = a.clr_name
FROM sys.assemblies a
JOIN sys.assembly_files af
    ON a.assembly_id = af.assembly_id
WHERE a.is_user_defined = 1 AND
    af.name = 'YourAssembly'

EXEC sys.sp_add_trusted_assembly
     @hash
    ,@description

相关问题