0

I have an exception when I use sql.data.client to query database sqlserver :

system.platformnotsupportedexception microsoft.data.sqlclient is not supported on this platform

My code source :

using (var connection = new SqlConnection(connectionString))
    {
        using (var cmd = new SqlCommand(queryString, connection))
        {
              cmd.Connection = connection;
              connection.Open();

              using (SqlDataReader reader = cmd.ExecuteReader())
              {
                   while (reader.Read())
                   {
                       return true;
                   }                               
              }
        }
    }
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
hbib
  • 21
  • 1
  • 1
  • 5
  • Go to project properties and set the target os to windows, and if it is a cross platform application then you need to use the correct nuget package – Muhammad Waqas Aziz Jun 09 '22 at 07:40
  • And in project properties also verify that your target framework is .Net 6 – Muhammad Waqas Aziz Jun 09 '22 at 07:42
  • 1
    Its a versioning issue with your Nuget Package, see here: https://stackoverflow.com/q/71022502/495455 and here https://github.com/dotnet/SqlClient/issues/289 and please include your research next time and what you've tried so we don't recommend things you've already tried. In this case a screenshot would be helpful. – Jeremy Thompson Jun 09 '22 at 07:42
  • @MuhammadWaqasAziz Thank you for your response py target Framework is .NET 6 – hbib Jun 09 '22 at 07:54
  • @JeremyThompson I followed the steps and i have other exeption : System.TypeInitializationException : 'The type initializer for 'Microsoft.Data.SqlClient.SqlConnection' threw an exception.' and FileNotFoundException : Could not load file or assembly 'System.Diagnostics.PerformanceCounter, Version=0.0.0.0, – hbib Jun 09 '22 at 08:01
  • Google it and try all the solutions and if none work, come back and ask a new question. https://www.google.com/search?q=%22Could+not+load+file+or+assembly+%27System.Diagnostics.PerformanceCounter%22 Your machine doesn't look like its in good shape, if you continue to get issues one after the other consider rebuilding your PC and starting off on the right foot. I'm not sure how you're getting these exceptions... – Jeremy Thompson Jun 09 '22 at 08:02
  • [later dup](https://stackoverflow.com/questions/72559921/system-data-sqlclient-is-not-supported-on-this-platform-in-net-6-0-c-sharp-libr) – SMor Jun 09 '22 at 12:37

1 Answers1

0

Is not supported on this platform error typically means you tried to load an x64 assembly in a x86 process or vice versa.

Microsoft.Data.SqlClient contains architecture specific files.

Double check your Nuget Package is for the correct architecture your machine supports.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • I use Any CPU so I think it did work for x86 and x64 – hbib Jun 09 '22 at 08:06
  • I have this exeption System.PlatformNotSupportedException : 'Microsoft.Data.SqlClient is not supported on this platform.' – hbib Jun 09 '22 at 09:02
  • If I have an class library calls `new SqlConnection()` and If I use this class library in a .exe file then I also get the same exception. Both class library and the c# console application is build using `any CPU` – Amogh Jul 25 '23 at 00:59
  • @hbib how you managed to solve this problem? – Amogh Jul 25 '23 at 00:59
  • I have changed the nuget from Microsoft.Data.SqlClient to system.data.sqlclient – hbib Jul 26 '23 at 13:20