Best way to use Microsoft.Data.SqlClient

Posts   
 
    
TomDog
User
Posts: 623
Joined: 25-Oct-2005
# Posted on: 07-Feb-2025 03:56:40   

What is the best way to switch from the default System.Data.SqlClient to Microsoft.Data.SqlClient?

For both framework and core, v5.11.

I got it working by overriding the SqlServerSpecificCreator like below but its a bit more involved than I expected so I'm wondering if there is something I'm missing:

using System.Data;
using System.Data.Common;
using System.IO;
using AQD.Helpers;
using Microsoft.Data.SqlClient;
using SD.LLBLGen.Pro.DQE.SqlServer;

namespace AQD.Model.SqlServer
{
  class AqdSqlServerSpecificCreator : SqlServerSpecificCreator
  {
    /// <inheritdoc />
    public override DbProviderFactory FactoryToUse => SqlClientFactory.Instance ?? base.FactoryToUse;

    /// <inheritdoc />
    protected override void SetParameterType(DbParameter parameter, string parameterType)
    {
      var sqlDbType = GeneralHelper.TryStringToEnum<SqlDbType>(parameterType);
      if (sqlDbType == null)
        base.SetParameterType(parameter, parameterType);
      else
        ((SqlParameter)parameter).SqlDbType = sqlDbType.Value;
    }
  }
}
Jeremy Thomas
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39826
Joined: 17-Aug-2003
# Posted on: 07-Feb-2025 09:10:30   

Specify the factory in the RuntimeConfiguration object. You can also use that on .net framework, see https://www.llblgen.com/Documentation/5.11/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/gencode_runtimeconfiguration.htm#dbproviderfactory

The factory you specify there is overwriting the factory obtained from the config file/machine.config.

Frans Bouma | Lead developer LLBLGen Pro
TomDog
User
Posts: 623
Joined: 25-Oct-2005
# Posted on: 13-Feb-2025 04:57:27   

Thanks for that. I had originally hoped it would be as easy as setting the providerName in the config connection string. I will next look at how I can do that.

As an aside Is there any easy way to search the documentation for a word or phrase? Best I could do to find that link was googling site: https://www.llblgen.com/Documentation "Microsoft.Data.SqlClient"

Jeremy Thomas
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39826
Joined: 17-Aug-2003
# Posted on: 13-Feb-2025 08:17:40   

TomDog wrote:

Thanks for that. I had originally hoped it would be as easy as setting the providerName in the config connection string. I will next look at how I can do that.

As an aside Is there any easy way to search the documentation for a word or phrase? Best I could do to find that link was googling site: https://www.llblgen.com/Documentation "Microsoft.Data.SqlClient"

The documentation has a search functionality which should work but I see anything with a '.' doesn't work... rage Searching for Microsoft SqlCient does work tho...

Frans Bouma | Lead developer LLBLGen Pro
TomDog
User
Posts: 623
Joined: 25-Oct-2005
# Posted on: 13-Feb-2025 22:06:10   

Otis wrote:

The documentation has a search functionality which should work but I see anything with a '.' doesn't work... rage Searching for Microsoft SqlCient does work tho...

You mean Microsoft SqlClientgrinning

Jeremy Thomas
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39826
Joined: 17-Aug-2003
# Posted on: 14-Feb-2025 08:21:00   

innocent mmmaaayyyybee

Frans Bouma | Lead developer LLBLGen Pro