'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?
Solution 1:[1]
.NET Core 2.0 now implements SqlDataAdapter and DataTable.
https://docs.microsoft.com/en-ca/dotnet/api/system.data.sqlclient.sqldataadapter?view=netcore-2.0
Solution 2:[2]
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
Solution 3:[3]
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.
Solution 4:[4]
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.
Solution 5:[5]
Use NuGet and install Microsoft.EntityFrameworkCore.SqlServer, the System.Data.SqlClient is buried there.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Randolph West |
Solution 2 | |
Solution 3 | Community |
Solution 4 | Andrew |
Solution 5 | Onceler |