2
Answers

read csv file and save it to database

Photo of Guest User

Guest User

4y
1.2k
1
hi, wanna ask
 
how to read from CSV file and save it to database row by row.
 
In the code below, it will open the CSV file and read the data row by row.
 
 foreach (string path in Directory.EnumerateFiles(@"C:\ipub\root", "*.csv"))
 {
   string[] lines = System.IO.File.ReadAllLines(path);
   foreach (string line in lines)
 {
   Console.WriteLine(line);
 }
 }
 
how to change this code, so that after it opens the CSV file and then read row by row after that save it to database.
 
Or there have another way to do it 
 
thank you
I using visual studio 2019, c#
 

Answers (2)

0
Photo of Guest User
Tech Writer 77 14.8k 4y
hi jay ,i appreciate your help.. but for right now, i can open CSV file and read row by row
the problem is, i dont know how to save it into database row by row
0
Photo of Jay Krishna Reddy
14 53.3k 5.4m 4y
Below is the code that I am sharing, I hope it helps you !!
  1. using System;  
  2. using System.Data;  
  3. using Microsoft.VisualBasic.FileIO;  
  4.   
  5. namespace ReadDataFromCSVFile  
  6.   {  
  7.     static class Program  
  8.       {  
  9.         static void Main()  
  10.         {  
  11.             string csv_file_path=@"C:\UsersAdministratorDesktoptest.csv";  
  12.             DataTable csvData = GetDataTabletFromCSVFile(csv_file_path);  
  13.             Console.WriteLine("Rows count:" + csvData.Rows.Count);              
  14.             Console.ReadLine();  
  15.         }  
  16.     private static DataTable GetDataTabletFromCSVFile(string csv_file_path)  
  17.         {  
  18.             DataTable csvData = new DataTable();  
  19.             try  
  20.             {  
  21.               using(TextFieldParser csvReader = new TextFieldParser(csv_file_path))  
  22.                  {  
  23.                     csvReader.SetDelimiters(new string[] { "," });  
  24.                     csvReader.HasFieldsEnclosedInQuotes = true;  
  25.                     //read column names  
  26.                     string[] colFields = csvReader.ReadFields();  
  27.                     foreach (string column in colFields)  
  28.                     {  
  29.                         DataColumn datecolumn = new DataColumn(column);  
  30.                         datecolumn.AllowDBNull = true;  
  31.                         csvData.Columns.Add(datecolumn);  
  32.                     }  
  33.                     while (!csvReader.EndOfData)  
  34.                     {  
  35.                         string[] fieldData = csvReader.ReadFields();  
  36.                         //Making empty value as null  
  37.                         for (int i = 0; i < fieldData.Length; i++)  
  38.                         {  
  39.                             if (fieldData[i] == "")  
  40.                             {  
  41.                                 fieldData[i] = null;  
  42.                             }  
  43.                         }  
  44.                         csvData.Rows.Add(fieldData);  
  45.                     }  
  46.                 }  
  47.             }  
  48.             catch (Exception ex)  
  49.             {  
  50.             }  
  51.             return csvData;  
  52.         }  
  53.       }  
  54.     }