How do I use System.Data in a .NET Core RC2 console app (Linux, Debian 8)?
Asked Answered
D

3

7

I've installed .NET Core RC2 on a Debian 8 amd64 system and would like to test if it's possible to query an instance of Microsoft SQL Server.

So I'd like to add to my project a dependency on the System.Data.SqlClient assembly.

Presently my project file created by running the dotnet new CLI tool looks like this:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0-rc2-3002702"
    }
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  }
}

Using this answer to a similar query, I was able to add a reference to System.Data.Common changing the

"frameworks": {
  "netcoreapp1.0": {
    "imports": "dnxcore50"
  }
}

fragment to

"frameworks": {
  "netcoreapp1.0": {
    "imports": "dnxcore50",
    "dependencies": {
      "System.Data.Common": "*"
    }
  }
}

which made dotnet restore use NuGet to download a bunch of stuff.

I then tried to change that fragment to read

"frameworks": {
  "netcoreapp1.0": {
    "imports": "dnxcore50",
    "dependencies": {
      "System.Data.SqlClient": "*"
    }
  }
}

but NuGet says it's

Unable to resolve 'System.Data.SqlClient' for '.NETCoreApp,Version=v1.0'.

If I change the version string to read "4.1.0-rc3-*" the error message just gets more specific:

Unable to resolve 'System.Data.SqlClient (>= 4.1.0-rc3)' for '.NETCoreApp,Version=v1.0'.

What I'm puzzled about is that the NuGet package gallery dedicated to .NET Core explicitly lists System.Data.SqlClient as available.

So what could I do to add a reference to System.Data.SqlClient assembly to my project and have NuGet download it?

On a side note, I'm currently playing around in a plain console with only the dotnet CLI tool. Is there any way to manage project dependencies for a .NET Core project without resorting to installing IDEs?

Daberath answered 17/5, 2016 at 18:7 Comment(3)
Not exactly sure what’s going on there, but try version "4.1.0-*". That will (as of now) for some reason get 4.1.0-rc2-24027 which works.Pavlov
@poke, that worked, thanks!Daberath
For those finding this post: I failed to make System.Data.SqlClient log into an instance of Microsoft SQL Server 2005 and filed an issue about this problem.Daberath
S
10

Like poke already annotated in the comment is correct. Specify a version to System.Data.SqlClient makes your restore happy ;)

Why is that? System.Data.SqlClient exists in the http://nuget.org gallery. Not specifying a version ("") is not allowed outside of the boundaries of a project (like a nuget feed package) and specifying solely an star "*" (you should never do that, it allows breaking changes) restore the highest available version. Since there is no stable, star will not find anything (there is some magic with the dashes behind). The RC2 version of that library is the mentioned 4.1.0-rc2-24027 and when you ask with 4.1.0-rc2-* it will take the highest of the RC2 builds (but there is only one). In comparison System.Data.Common has a public release on nuget.org for the Universal Windows Platform and is found for that reason.

The RC3 is the next release and only available on developer feeds from the .NET Core and ASP.NET Core team and not the public nuget feed. You should not play with them.

Susie answered 17/5, 2016 at 19:36 Comment(5)
Thank you (and @poke): it's good to know the proposed solution is not magic but the expected behaviour.Daberath
A quick addition to this answer: The default feed is not myget but the “normal” nuget feed. The star ("*") version dependency will always get the latest stable version of a package. So for System.Data.Common, you get 4.0.0; there isn’t a stable System.Data.SqlClient though, so that fails. You can confirm which version is actually used by looking at the project.lock.json after doing dotnet restore.Pavlov
Do I understand correctly that should I have another project side-by-side with the current one, which would require another version of System.Data.SqlClient, NuGet will be able to fetch that another version, too, keep them both in whatever place it keeps them and the project build tools will be able to pick the listed specific version for each project? (Not familiar with NuGet, sorry).Daberath
NuGet in v3 has some rules around siblings etc. see docs.nuget.org/consume/ProjectJson-DependencySusie
@Daberath Yes, you can have different projects require different versions of dependencies and dotnet restore will make sure that the correct versions are available for each project. The packages are locally cached in %HOME%\.nuget\packages, or fetched as necessary. The whole beauty of .NET Core is that you only depend on what you need instead of always shipping with the full .NET Framework which only comes in few versions (and upgrades affecting all applications running on it).Pavlov
J
1

if you are in project.json file, the intellisense guide you now if you have updated the Visual studio with the latest tooling avaiable..

I added the following in the dependencies element and it works perfectly..

"System.Data.SqlClient": "4.1.0-rc2-24027",

Judiciary answered 1/6, 2016 at 20:39 Comment(0)
C
0

On my MINT 19 Tara I do not use System.Data.SqlClient but the newer version Microsoft.Data.SqlClient. Check nuget

After typing the .NET-cli command, the package was downloaded and the reference was added to the project csproj file.

In the code, use the newer namespace:

using Microsoft.Data.SqlClient;

The "regular" code works fine:

 public static void CreateCommand(string queryString, string connectionString)
    {
        using (SqlConnection connection = new SqlConnection(
                   connectionString))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            command.Connection.Open();
            SqlDataReader reader = command.ExecuteReader() ;
            while( reader.Read() ) {
                for (int i = 0; i < reader.FieldCount; i++)
                {
                   Console.WriteLine( "Column name={0}, Value={1}", 
                        reader.GetName(i),
                        reader.GetValue(i) ); 
                }
            }
        }
    }
Cenac answered 28/5, 2019 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.