.Net Core how to implement SQLAdapter ./ DataTable function

后端 未结 4 2010
没有蜡笔的小新
没有蜡笔的小新 2020-12-31 04:51

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 ar

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 05:49

    You can use DbDataAdapter link for more info

    Then use this function for CreateDataAdapter

    private static DbDataAdapter CreateDataAdapter(this DbConnection connection, DbCommand command)
    {
         var adp = DbProviderFactories.GetFactory(connection).CreateDataAdapter();
         adp.SelectCommand = command;
         return adp;
     }
    

    Then normal can use like code below

    var connection = Context.Database.GetDbConnection();
    using var command = connection.CreateCommand();
    command.CommandText = cmdText;
    using DbDataAdapter adp = connection.CreateDataAdapter(command);
    

    After you can fill

    using DataSet dataSet = new DataSet();
    adp.Fill(dataSet);
    

提交回复
热议问题