Error Database Target[database]: Error initializing target Exception: Could not load file assembly System.Data.SqlClient

Today in this article, we will see how to fix error ” Error Database Target[database]: Error initializing target”.

We will cover below aspects,

Issue Description

I got the error while performing NLog config setup for database logging gives below error,

Error Database Target[database]: Error initializing target Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Data.SqlClient, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

Similar error could also produce for another database provider like,

  • System.Data.OracleClient
  • Oracle.DataAccess.Client
  • System.Data.SQLite
  • Npgsql
  • MySql.Data.MySqlClient

However, you should follow a similar below explained approach to resolve the issue.

Resolution

The above issue I found to be due to not specifying required dependency in the form of the NuGet package in the project.

I recommend you go through the article on Database logging using NLog while applying the below fix.

Please install Nuget-package ‘System.Data.SqlClient’ or the provider you are missing for the DbProvider.

Step1 : Please install Nuget package from Package manager Console

PM>Install-Package System.Data.SqlClient -Version 4.8.1

Or Install it from from Nuget package Manager,

Config Database Targetdatabase Error initializing

Step2:

Under <Target> block , please mention the target type as ‘Database

<target name="database" xsi:type="Database" >

Step3:

Note: For the above error without specifying the provider i was able to resolve the error.

If not using SQL provider, then please use fully qualified name of the provider connection type.

Example:

MySQL Client

dbProvider="MySql.Data.MySqlClient.MySqlConnection, MySqlConnector"

SQL Client

dbProvider="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient"

SQLite client

dbProvider="Microsoft.Data.Sqlite.SqliteConnection, Microsoft.Data.Sqlite"

Thats All,

Did I miss anything else in these resolution steps?

Did the above steps resolve your issue? Please sound off your comments below!

Reference :



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *