简体   繁体   中英

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

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?

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.

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",

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) ); 
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM