.Net Core如何实现SQLAdapter ./DataTable函数 [英] .Net Core how to implement SQLAdapter ./ DataTable function

查看:382
本文介绍了.Net Core如何实现SQLAdapter ./DataTable函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的.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 。

您必须使用VS2017 Preview 15.3,目标是.net core 2.0,并为 System.Data.Common 以及 System.Data.SqlClient 。下面的代码。

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.

请参见 https://blogs.msdn.microsoft.com/devfish/2017/05/15/exploring-datatable-and-sqldbadapter-in -asp-net-core-2-0 / 了解更多信息。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆