[英]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.我正在尝试在 .NET Core 1.0 项目中使用 ADO.NET 连接到没有实体框架的数据库。
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.如您所见,我可以使用 System.Data.SqlClient,但由于某种原因编译器说缺少 SqlDataAdapter。
What can I do?我能做什么? It can be fixed installing another packages from NuGet?
可以修复从 NuGet 安装另一个包吗?
.NET Core 2.0 now implements SqlDataAdapter and DataTable. .NET Core 2.0 现在实现了 SqlDataAdapter 和 DataTable。
https://docs.microsoft.com/en-ca/dotnet/api/system.data.sqlclient.sqldataadapter?view=netcore-2.0 https://docs.microsoft.com/en-ca/dotnet/api/system.data.sqlclient.sqldataadapter?view=netcore-2.0
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
.对于 .net core 3.1,微软提供了一个新的库
Microsoft.Data.SqlClient
,它的代码几乎与原始System.Data.SqlClient
相同。 You just need to have the new NuGet package, and all the code should work with very minimum changes (if any).您只需要拥有新的 NuGet 包,并且所有代码都应该以最少的更改(如果有)工作。
The mainstream ORMs, Dapper and EntityFramework, also depend on this package for the underlying work.主流的 ORM,Dapper 和 EntityFramework,也依赖这个包来做底层的工作。
The updated version is 2.0 as of July 2020 ( https://www.nuget.org/packages/Microsoft.Data.SqlClient/ )截至 2020 年 7 月,更新版本为 2.0 ( 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
但是,如果要使用原始包,则需要将项目版本设置为
Platform Extensions 3.1
It appears .net core does not provide implementations for SqlDataAdapter
and even DataTable
.net core 似乎没有提供
SqlDataAdapter
甚至DataTable
This is quote from this page这是从这个页面引用
Regarding
关于
DataTable and DataSet and also SqlDataAdapter are not found…?!?
没有找到 DataTable 和 DataSet 以及 SqlDataAdapter ......?!?
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.
至于 .NET Core 1.0 发布的数据访问技术 > 仅限于低级?ADO.NET 接口(IDbConnection、IDbCommand 等)或丰富的 EF Core。 >Nevertheless, you can use 3rd party libraries that may be used as replacement ?for DataRow/DataTable/DataAdapter, for example NReco.Data .
> 尽管如此,您可以使用可用作 DataRow/DataTable/DataAdapter 替代品的 3rd 方库,例如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.我为 .net core 2.0 重新编译了 MySQL.Data 并编译了 mysql dataadapter 和 commandbuilder。到目前为止我测试过的都是有效的。
https://github.com/amjtech/MySQL.Data https://github.com/amjtech/MySQL.Data
Enjoy.享受。
使用 NuGet 并安装 Microsoft.EntityFrameworkCore.SqlServer,System.Data.SqlClient 就埋在那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.