How to read data from a file, and then check data and insert data into database operations asynchronously

I have the following three methods in my Windows service for converting CSV data files to an Sql server database.

  • Method 1. To read data from a CSV file (each line contains one Employee information)

  • Method 2: to verify data using employee ID, name, etc.

  • Method 3. Insert data into a database table.

Currently, I am reading one line from the CSV file and calling the three methods described above one after the other (which means that after the completion of method 1 I will start method 2, and after the completion of method 2 I will start method 3. Then I will read another line from the file again CSV and repeat the same steps)

Since this is a synchronous process, it takes a long time to process one file with more than ten thousand records.

If I want to make this an asynchronous process, how would I call my methods asynchronously and which C # classes should I use to achieve this?

+4
source share
2 answers

To do this, it is best to create your class in the code by creating a list of your class. When checking, add verified data to this list, but not verified, you can put them in a line or another list to show that the rest was not inserted.

List of Accepted Employees

public List<Employee> acceptedEmployees = new List<Employee>();

Employee Class

class Employee {
   public int ID{ get; set; }
   public string Name{ get; set; }

   public Employee(int ID, string name)
        {
           ID = ID;
           Name = name;
        }
}

, .

, 1 Connection , , .

, , 500 , :

INSERT INTO mytable (ID, NAME) VALUES (1,'Dan'),(2,'Stack'),(3,'Whatever');

, .

+1
string[] csvLines = File.ReadAllLines(@"c:\employees.csv")

List<Task> tasks = new List<Tasks>();

foreach(var line in csvLines )
{//spawn tasks to parse, validate , save
   var task = Task<ValidationResult>.Factory.StartNew(()=>
     {//async task start

         var validationResult = Validate(line);
         if(validationResult.IsValid)
         {
            Save(validationResult.Employee);
         }     

         return validationResult; 

     });//async task end

     tasks.Add(task);

}

//wait for results
var allResults = new List<ValidationResult>();    
foreach(var t in tasks)
{
 allResults.Add(t.Result);
}
0

Source: https://habr.com/ru/post/1529269/


All Articles