D
D
Dmitry2018-11-22 21:57:11
SQLite
Dmitry, 2018-11-22 21:57:11

How to process a selection from the database line by line?

There is a SQLite database. There is a class which works with base which I write on C#. It is necessary that I call the method of this class, it would make a selection from the base and perform actions in the program on each result (line from the selection).
If you count all the lines at once and return them to the program, then after all, they will load my entire RAM.
Passing it as a parameter in order to pull out not everything at once, but let's say 100 lines each, seems like some sort of fencing of the garden and complication.
Passing a method reference to a class (to read a string and call the passed method to process it) doesn't seem right. What is the best way to deal with such a situation?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sumor, 2018-11-24
@Sumor

Even so

using(var reader = sqliteCommand.ExecuteReader())
{
  while(reader.Read())
  {
    // вызов вашего метода для конкретной строчки
  }
}

Even so
using(var reader = sqliteCommand.ExecuteReader())
{
  // вызов вашего метода для reader, где вы будете сами перебирать строчки  
}

And if you want to play around with delegates (if, for example, you have several options for processing the methods of the entire database),
then you pass at least a banal Action (including in the form of a lambda expression) to the reading function and ...
void Do()
{
  DoSmthWithDB((Action<DataReader>)((reader) => {Console.WriteLine(reader[0].ToString());}));
}

void DoSmthWithDB(Action<DataReader> myAction)
{
...
// Создание команды к БД
...
using(var reader = sqliteCommand.ExecuteReader())
{
  while(reader.Read())
  {
    myAction(reader); // Action<DataReader> переданный в виде параметра
  }
}
}

R
Rsa97, 2018-11-22
@Rsa97

Usually, callbacks are used for this, if I'm not mistaken, then in terms of C # these are delegates (delegate)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question