Hasibul Kamel

Hasibul Kamel

  • NA
  • 44
  • 4.2k

save csv file in database

Aug 20 2020 8:45 PM
Hello and hi, why this got does not execute if condition ? it goes straight to else even though the file name already in database. I need to do if the csv file name already save in database, it will print out file exist. please help me
  1. foreach(FileInfo file in filess) {  
  2.   DateTime lastWriteTime = file.LastWriteTime;  
  3.   
  4.   Console.WriteLine("File list : " + file);  
  5.   Console.WriteLine("Time last Edit : " + lastWriteTime);  
  6.   
  7.   if (File.Exists("file")) {  
  8.     Console.WriteLine("File Name Exist!");  
  9.     var sel = "select * from listfile where filename = @filename";  
  10.     var cmd2 = new NpgsqlCommand(sel, conn);  
  11.     conn.Open();  
  12.   
  13.     cmd2.Parameters.AddWithValue("@filename", file);  
  14.     Console.WriteLine(file + " exist in database.");  
  15.     cmd2.ExecuteNonQuery();  
  16.     conn.Close();  
  17.   }  
  18.   else {  
  19.     Console.WriteLine("File Name NOT Exist!");  
  20.   
  21.     var lf = "insert into listfile(filename)values(@filename)";  
  22.     var cmd = new NpgsqlCommand(lf, conn);  
  23.     conn.Open();  
  24.   
  25.     cmd.Parameters.AddWithValue("filename", Convert.ToString(file));  
  26.     string filename1 = cmd.Parameters["@filename"].Value.ToString();  
  27.     Console.WriteLine("Record inserted successfully. filename = " + file);  
  28.   
  29.     cmd.ExecuteNonQuery();  
  30.     conn.Close();  
  31.   }  
  32. }  

Answers (4)