简体   繁体   中英

SqlDataAdapter missing in a ASP.NET Core project

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?

For .net core 3.1, Microsoft provides a new library as Microsoft.Data.SqlClient which has almost all code 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 2.0 as of July 2020 ( 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

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 .

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.

使用 NuGet 并安装 Microsoft.EntityFrameworkCore.SqlServer,System.Data.SqlClient 就埋在那里。

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