Getting error on executing CLR procedure in sql server - sql-server

I am using the microsoft sql server 2008. On executing the CLR procedure i am getting the below error.
An error occurred in the Microsoft .NET Framework while trying to load
assembly id 65547. 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
'clrprocedure, 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.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)
Please suggest.

After a long R&D, i got the solution.
The TRUSTWORTHY property of the database was turned OFF previously.
I turned it ON and now it is working fine for me.

Related

Re Create Assembly used by SSIS when deploying Package

Receiving the below message when deploying packages via SSIS to SQL Server 2019.
Seems that an assembly is missing, is there a way to create this?
TITLE: SQL Server Integration Services
------------------------------
An error occurred in the Microsoft .NET Framework while trying to load assembly id 65536. The server may be running out of resources, or the assembly may not be trusted. 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 'microsoft.sqlserver.integrationservices.server, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. An error relating to security occurred. (Exception from HRESULT: 0x8013150A)
System.IO.FileLoadException:
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)
(Microsoft SQL Server, Error: 10314)
Tried re-creating the integration services catalogue which doesn't help.

Issue Deploying SSIS Package

I am working at a small company with no DBA but we use SQL Server for reporting. I'd like to get SSIS up and running for some ETL and back end processes. I used to develop in the past so have some basic skills but no DBA experience at all. I've been able to get SSIS installed at least I see the integration services folders and the SSIS database. But after working through what I thought were security issues and getting CLR enabled I'm receiving the following message. Thought I'd ask this group as I start to research to see if anyone had a quicker answer.
We are using SQL Server version 2019, Visual Studio 2019 and SSMS version 15.
TITLE: SQL Server Integration Services
An error occurred in the Microsoft .NET Framework while trying to load assembly id 65536. The server may be running out of resources, or the assembly may not be trusted. 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 'microsoft.sqlserver.integrationservices.server, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. An error relating to security occurred. (Exception from HRESULT: 0x8013150A)
System.IO.FileLoadException:
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)
(Microsoft SQL Server, Error: 10314)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=15.00.2000&EvtSrc=MSSQLServer&EvtID=10314&LinkId=20476
The SSIS is installed using the same installation package as the sql server.Sql server version and SSIS version mismatch and thats why the SSISDB is having issues.

The path for 'ISServerExec.exe' cannot be found when deploying a project

I am working on jobs currently and when I want to deploy my project I am receiving the following error. I tried to check the DTSPath but I wasn't able to find it under the following path "go to regedit->HKEY_LOCAL_MACHINE->SOFTWARES and under Microsoft Sql Server in SSIS"
The path for 'ISServerExec.exe' cannot be found. The operation will
now exit. A .NET Framework error occurred during execution of
user-defined routine or aggregate "deploy_project_internal":
System.Data.SqlClient.SqlException: The path for 'ISServerExec.exe'
cannot be found. The operation will now exit.
System.Data.SqlClient.SqlException: at
System.Data.SqlClient.SqlConnection.OnError(SqlException exception,
Boolean breakConnection, Action`1 wrapCloseInAction) at
System.Data.SqlClient.SqlInternalConnectionSmi.EventSink.DispatchMessages(Boolean
ignoreNonFatalMessages) at
System.Data.SqlClient.SqlCommand.RunExecuteNonQuerySmi(Boolean
sendToPipe) at
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource
1 completion, String methodName, Boolean sendToPipe, Int32 timeout,
Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at
System.Data.SqlClient.SqlCommand.ExecuteToPipe(SmiContext pipeContext)
at Microsoft.SqlServer.Server.SqlPipe.ExecuteAndSend(SqlCommand
command) at
Microsoft.SqlServer.IntegrationServices.Server.ServerConnectionControl.RaiseError(SysMessageId
messageId, SysMessageSeverity severity, Object[] args) at
Microsoft.SqlServer.IntegrationServices.Server.ServerApi.DeployProjectInternal(SqlInt64
deployId, SqlInt64 versionId, SqlInt64 projectId, SqlString
projectName) . (Microsoft SQL Server, Error: 27108)
This solved my problem immediately:
http://thesqlgirl.com/2017/10/25/ssis-error-the-path-for-isserverexec-exe-cannot-be-found/
When I re-ran the SQL Installer and selected the instance I was having trouble with, I found that SSIS (Integration Services) wasn't installed, just like the article above suggested. Somehow, when I had removed a secondary instance of SQL that was running on same server, it affected other instances.
Not shocking for a Microsoft product.

LocalDB .mdf file not attached automatically with AttachDbFileName in connection string

I'm expecting this behavior because of this MSDN article about the AttachDbFileName property
The first time you connect to a database by using this option in the
connection string, the SQL Server Express or LocalDB instance attaches
the database, and it stays attached. When you want to connect to the
same database in the future you could use Initial Catalog without
AttachDbFileName if you prefer.
I have a connection string that is used by Entity Framework:
<add name="DatabaseContext" providerName="System.Data.SqlClient" connectionString="Server=(localdb)\MSSQLLocalDB;Database=DatabaseName;Integrated Security=True;AttachDbFileName=|DatabaseName|\DatabaseName.mdf" />
This works fine after I open the database in SQL Management Studio feeding it the AttachDbFileName value with the absolute path as an additional prameter.
But if I don't attached it with SQL Management Studio first then I'm getting the following error when trying to run EF migrations on the database:
System.Data.SqlClient.SqlException (0x80131904): Cannot create file 'C:\Users\user\Desktop\folder\DatabaseName.mdf' because it already exists. Change the file path or the file name, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext`1 c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass1a.<CreateDatabaseFromScript>b__19(DbConnection conn)
at System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass33.<UsingConnection>b__32()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
at System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action`1 act)
at System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action`1 act)
at System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable`1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript)
at System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)
at System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)
at System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase()
at System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection)
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update()
This looks as if it doesn't attach the database automatically for some reason and then tries to create it as if the file didn't exist.
Is there an additional connection string option or LocalDB configuration that can fix this so that the database is attached automatically on the first conneciton?
Reviewing the Web.Config may help to understand what happened here. The LocalDB is a database that is meant to serve as a file. If you have an actual database you could change the connection string. Either way, check the version of your localdb (example 2019 would be version 15 of sql server and version 13 of the localdb). If the one in the connection string (web.config) points to a different location, it is a task for editing the connection string.
https://www.connectionstrings.com/store-connection-string-in-webconfig/
The answer is trivial: the .mdf and .ldf files were created in a newer version of SQL server so the older version was not attaching them as if they didn't exist.

FileLoadException / Msg 10314 Error Running CLR Stored Procedure

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)
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
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.
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
Does your assembly do file I/O? If so, you must grant the assembly permission to do this. In SSMS:
Expand "Databases"
Expand the node for your database
Expand "Programmability"
Expand "Assemblies"
Right-click your assembly, choose Properties
On the "General" page, change "Permission set" to "External access"
ALTER AUTHORIZATION ON DATABASE::mydb TO sa;
ALTER DATABASE [myDB] SET TRUSTWORTHY ON
GO

Resources