Hasibul Kamel

Hasibul Kamel

  • NA
  • 44
  • 3.8k

compare csv records in database and add new line

Sep 3 2020 11:44 AM
Hi, wanna ask, how to compare CSV from CSV file and database? I using lastwritetime to see changes in CSV. If lastwritetime same, it means there are no new records in the CSV file. If lastwritetime got changes, it means there are new records added in CSV file and I want to add it to database without duplicate the records. Means only insert the new records into database. Help me please.
  1. var lwt = "select COUNT(*) from listfile where lastwritetime=@lastwritetime";  
  2. var cmd2 = new NpgsqlCommand(lwt, conn);  
  3. cmd2.Parameters.AddWithValue("lastwritetime", Convert.ToDateTime(lastWriteTime));  
  4. int LWT = Convert.ToInt32(cmd.ExecuteScalar());  
  5. cmd2.ExecuteNonQuery();  
  6.   
  7. if (LWT > 0)  
  8. {  
  9.     Console.WriteLine("There are no changes in lastWriteTime CSV file");  
  10. }  
  11. else  
  12. {  
  13.     Console.WriteLine("There are changes lastWriteTime in CSV file");  
  14.   
  15.     var lf2 = "insert into listfile(filename,lastwritetime)values(@filename,@lastwritetime)";  
  16.     var cmd3 = new NpgsqlCommand(lf2, conn);  
  17.   
  18.     cmd3.Parameters.AddWithValue("filename", Convert.ToString(file));  
  19.     string filename = cmd3.Parameters["@filename"].Value.ToString();  
  20.     Console.WriteLine("Record inserted successfully. filename = " + file);  
  21.   
  22.     cmd3.Parameters.AddWithValue("lastWriteTime", Convert.ToDateTime(lastWriteTime));  
  23.     string time = cmd3.Parameters["@lastwritetime"].Value.ToString();  
  24.     Console.WriteLine("Record inserted successfully. lastWriteTime = " + lastWriteTime);  
  25.     cmd3.ExecuteNonQuery();  
  26. }

Answers (3)