none
C# Using an CSV file RRS feed

  • Question

  • I am fairly new to using C# so code doesn't need to be difficult

    I need to input the data from a CSV file into the program and be able to add a record to the end of it before saving it back to a CSV file. I am struggling to find a way to do all 3 of these. I am able to do it in Visual Basic really easily by inputting each line in a loop, adding 1 to a counter and then adding it to an array, this counter can then be used to add the new data. I also used a function in the loop; Do Until EOF(Filename) which doesn't seem to exist in C#Help would be appreciated 

    Thank you

    Aiden

    Saturday, April 14, 2018 3:29 PM

All replies

  • Hi Aidenlvr,

    Thank you for posting here.

    >>I need to input the data from a CSV file into the program

    For your question, how do you want to read the data from csv file? If you want to read the data from csv file to a dataTable, you could refer to the code below.

    static void Main(string[] args)
            {
    
                string csv_file_path = @"test.csv";
                DataTable csvData = GetDataTabletFromCSVFile(csv_file_path);
                Console.ReadLine();
            }
    
            private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
            {
                DataTable csvData = new DataTable();
                try
                {
                    using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
                    {
                        csvReader.SetDelimiters(new string[] { "," });
                        csvReader.HasFieldsEnclosedInQuotes = true;
                        string[] colFields = csvReader.ReadFields();
                        foreach (string column in colFields)
                        {
                            DataColumn datecolumn = new DataColumn(column);
                            datecolumn.AllowDBNull = true;
                            csvData.Columns.Add(datecolumn);
                        }
                        while (!csvReader.EndOfData)
                        {
                            string[] fieldData = csvReader.ReadFields();
                            //Making empty value as null
                            for (int i = 0; i < fieldData.Length; i++)
                            {
                                if (fieldData[i] == "")
                                {
                                    fieldData[i] = null;
                                }
                            }
                            csvData.Rows.Add(fieldData);
                        }
                    }
                }
                catch (Exception ex)
                {
                }
                return csvData;
    
            }

    >> be able to add a record to the end of it before saving it back to a CSV file.

    What record you want to add? Please provide more details about this.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, April 16, 2018 7:02 AM
    Moderator