Unable to Load DLL 'sni.dll'

How can the 'Unable to load DLL 'sni.dll'. The specified module could not be found (0x8007007e)' error be resolved when using Microsoft.Data.SqlClient in workflows or custom code?

Issue Description

This article addresses the error "Unable to load DLL 'sni.dll' or 'System.DllNotFoundException: Unable to load DLL 'Microsoft.Data.SqlClient.SNI.dll' or one of its dependencies: The specified module could not be found. (0x8007007E) '. The specified module could not be found. (0x8007007e)." The error typically occurs when attempting to interact with databases using Microsoft.Data.SqlClient in UiPath or other environments. The issue stems from missing native networking libraries required by the Microsoft.Data.SqlClient package.

Root Cause

The Microsoft.Data.SqlClient NuGet package separates managed .NET assemblies and native networking libraries (sni.dll). Managed dependencies are provided in the base package (Microsoft.Data.SqlClient), but native binaries responsible for handling database connectivity are packaged separately in the Microsoft.Data.SqlClient.SNI.Runtime NuGet package.

By design, native libraries are located within the runtimes/{RID}/native/ folder of the associated NuGet package and are typically loaded automatically during execution. However, improper configuration or omission can lead to this error.

Due to this non-standard dependency packaging structure, the runtime fails to access the native networking library (sni.dll), resulting in the error message.

Symptoms

  • The error message "Unable to load DLL 'sni.dll'. The specified module could not be found. (0x8007007e)" appears during database operations.
  • SQL connection workflows fail in UiPath or through custom code using Microsoft.Data.SqlClient.

Resolution

Solution 1: Manual DLL Loading via Code

Explicitly loading the native library in workflows or scripts can resolve this issue. Use the following code snippet to load the native library before initiating database operations.

  • Example for Microsoft.Data.SqlClient 6.0.1:

var managedDllPath = typeof(Microsoft.Data.SqlClient.SqlCommand).Assembly.Location;

var nativeDllPath = Path.Combine(managedDllPath, "../../../../../../../microsoft.data.sqlclient.sni.runtime/6.0.2/runtimes/win-x64/native/Microsoft.Data.SqlClient.SNI.dll");

System.Runtime.InteropServices.NativeLibrary.Load(nativeDllPath);

  • Example for Microsoft.Data.SqlClient 4.0.0:

var managedDllPath = typeof(Microsoft.Data.SqlClient.SqlCommand).Assembly.Location;

var nativeDllPath = Path.Combine(managedDllPath, "../../../../../../../microsoft.data.sqlclient.sni.runtime/4.0.0/runtimes/win-x64/native/Microsoft.Data.SqlClient.SNI.dll");

System.Runtime.InteropServices.NativeLibrary.Load(nativeDllPath);

Solution 2: Place the DLL in UiPath Installation Folder

Manual placement of the sni.dll file is another option to resolve the error. Download the required sni.dll from the Microsoft.Data.SqlClient.SNI.Runtime NuGet package and place it in the UiPath Studio installation directory.

Steps:

  1. Locate the Microsoft.Data.SqlClient.SNI.Runtime package for the relevant library version.
  2. Extract the sni.dll file located in runtimes\win-x64\native\.
  3. Copy the sni.dll file to C:\Program Files\UiPath\Studio.

Solution 3: Use UiPath.Database.Activities Package

The UiPath.Database.Activities package provides specialized tools to manage database interactions without requiring manual configuration of sni.dll. Using this package reduces the complexities associated with dependency management and ensures thw smooth execution of workflows.

Steps:

  1. Install the UiPath.Database.Activities package in the project.
  2. Configure database connections using the provided activity options.