2

I'm trying to get a connection to a database without Entity Framework, using ADO.NET in a .NET Core 1.0 project.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ASP_NETCore3.Models;
using System.Data;
using System.Data.SqlClient;
//using System.Configuration;

namespace ASP_NETCore3.Repository
{
    public class LineasRepository
    {
        private SqlConnection con;

        private void connection()
        {
            //TODO: Implementar variables de confuracion obtiendolas desde appsettings.json
            string constr = "Data Source=mylocaldb\\sql08;Initial Catalog=empresas;User id=user;Password=secret;Integrated Security=SSPI;";
            con = new SqlConnection(constr);
        }

        public List<LineasModel> GetAllLineas()
        {
            connection();
            List<LineasModel> LineasList = new List<LineasModel>();
            SqlCommand com = new SqlCommand("select * from CATLIN", con);
            com.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataTable dt = new DataTable();
            con.Open();
            da.Fill(dt);
            con.Close();

            LineasList = (from DataRow dr in dt.Rows

                       select new LineasModel()
                       {
                           cod_lin = Convert.ToInt32(dr["COD_LIN"]),
                           nom_lin = Convert.ToString(dr["NOM_LIM"]),
                           margen = Convert.ToString(dr["MARGEN"]),
                       }).ToList();


            return EmpList;
        }
    }
}

As you can see, I can use System.Data.SqlClient, but for some reason compiler says that SqlDataAdapter is missing.

What can I do? It can be fixed installing another packages from NuGet?

3
  • you will need to add reference to the required assembly in project.json Commented Jul 22, 2016 at 22:59
  • Apparently I'll have to use another approach, I'll try with Micro-ORM Dapper. Commented Jul 22, 2016 at 23:15
  • 1
    @Mr_LinDowsMac consider using NReco.Data library ( github.com/nreco/data ), it also offers API for schema-less data access. And as a bonus, you can use db-independent abstract queries instead of composting SQL in the code. Commented Jul 23, 2016 at 11:37

5 Answers 5

4

.NET Core 2.0 now implements SqlDataAdapter and DataTable.

https://learn.microsoft.com/en-ca/dotnet/api/system.data.sqlclient.sqldataadapter?view=netcore-2.0

Sign up to request clarification or add additional context in comments.

1 Comment

try explain content as much as possible within answer, this is much of only external URL which best suits as comment
4

For .net core 3.1, Microsoft provides a new library as Microsoft.Data.SqlClient which has almost all code the same as the original System.Data.SqlClient. You just need to have the new NuGet package, and all the code should work with very minimum changes (if any).

The mainstream ORMs, Dapper and EntityFramework, also depend on this package for the underlying work.

The updated version is 4.1 as of Apr 2022 (https://www.nuget.org/packages/Microsoft.Data.SqlClient/)

However, if you want to use the original package, you need to have the project version set to Platform Extensions 3.1

Comments

3

Use NuGet and install Microsoft.EntityFrameworkCore.SqlServer, the System.Data.SqlClient is buried there.

Comments

1

It appears .net core does not provide implementations for SqlDataAdapter and even DataTable

This is quote from this page

Regarding

DataTable and DataSet and also SqlDataAdapter are not found…?!?

as for .NET Core 1.0 release data access technologies >are limited to low-level ?ADO.NET interfaces (IDbConnection, IDbCommand etc) or rich EF Core. >Nevertheless, you can use 3rd party libraries that may be used as replacement ?for DataRow/DataTable/DataAdapter, for example NReco.Data.

Comments

0

I recompiled MySQL.Data for .net core 2.0 with the mysql dataadapter and commandbuilder compiled in. What I have tested so far works.

https://github.com/amjtech/MySQL.Data

Enjoy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.