.Net Core 如何实现SQLAdapter ./DataTable 功能 [英] .Net Core how to implement SQLAdapter ./ DataTable function
问题描述
我有一个简单的 .Net Framework 例程,它运行一个查询并返回一个 DataTable 对象.我需要将它移植到 .Net Core,但是我推断不支持 SQLAdapter 和 DataTable
I have a simple .Net Framework routine which runs a query and returns a DataTable object. I need to port this to .Net Core, however I infer that SQLAdapter and DataTable are not supported
SqlConnection con = new SqlConnection(m_ConnectString);
SqlCommand cmd = new SqlCommand(strQuery);
SqlDataAdapter sda = new SqlDataAdapter();
// assign the transaction and connection to the command object
cmd.Connection = con;
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
// execute query and soak up results
sda.Fill(dt);
return dt;
谁能建议我如何使用支持的内容重新实现此代码?谢谢
Can anyone suggest how I can reimplement this code, using what is supported ? Thanks
推荐答案
SqlDBAdapter
和 DataTable
现在得到支持.
SqlDBAdapter
and DataTable
are now supported.
您必须使用 VS2017 Preview 15.3,目标是 .net core 2.0,并为 System.Data.Common
和 System.Data.SqlClient
添加 NuGet 包.代码如下.
You must use VS2017 Preview 15.3, target .net core 2.0, and add NuGet packages for System.Data.Common
as well as System.Data.SqlClient
. Code below.
public static DataTable ExecuteDataTable(SqlConnection conn, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
DataTable dt = new DataTable();
dt.Columns.Add("CustomerID");
dt.Columns.Add("CustomerName");
SqlDataReader dr = ExecuteReader(conn, cmdType, cmdText, cmdParms);
while (dr.Read())
{
dt.Rows.Add(dr[0], dr[1]);
}
return dt;
}
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 如何实现SQLAdapter ./DataTable 功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!