在 .NET Core 中使用 DataTable [英] Using DataTable in .NET Core
问题描述
我在 SQL Server 中有一个接受用户定义表类型的存储过程.我正在关注这篇文章的答案 从 C# 列表批量插入 SQL Server 到带有外键约束的多个表中 关于如何将 DataTable 发送到 SQL 中的存储过程.
I have a stored procedure in SQL Server that accepts a User-Defined Table Type. I'm following the answer from this post Bulk insert from C# list into SQL Server into multiple tables with foreign key constaints on how to send a DataTable to a stored procedure in SQL.
但是当我创建 DataTable table = new DataTable();
我得到一个错误 DataTable 不包含一个带有 0 个参数的构造函数
.
But when I create DataTable table = new DataTable();
I get an error that DataTable does not contain a constructor that takes 0 arguments
.
我发现了这个 https://github.com/VahidN/EPPlus.Core/issues/4 基本上说 DataTable
在 .NET Core 中不再支持.那么现在怎么办?我如何创建一个 DataTable(或者它的替代品是什么)?如何将用户定义的表类型发送到 .NET Core 上的 SQL Server?
I found this https://github.com/VahidN/EPPlus.Core/issues/4 which basically saying DataTable
is no longer supported in .NET Core. So now what? how do I create a DataTable (or what is it's replacement)? how do I send a User-Defined Table Type to SQL Server on .NET Core?
推荐答案
DataTable 现在在 .NET CORE 2.0 中得到支持.请参阅我在 .Net Core 如何实现 SQLAdapter 的回答./DataTable功能.下面的示例代码适用于 2.0.
DataTable is now supported in .NET CORE 2.0. See my answer at .Net Core how to implement SQLAdapter ./ DataTable function . Sample code below works in 2.0.
public static DataTable ExecuteDataTableSqlDA(SqlConnection conn, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
System.Data.DataTable dt = new DataTable();
System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter(cmdText, conn);
da.Fill(dt);
return dt;
}
这篇关于在 .NET Core 中使用 DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!