问题
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?
回答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
回答2:
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.
回答3:
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.
回答4:
Use NuGet and install Microsoft.EntityFrameworkCore.SqlServer, the System.Data.SqlClient is buried there.
来源:https://stackoverflow.com/questions/38536339/sqldataadapter-missing-in-a-asp-net-core-project