Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey Guys,

I need to read a CSV file in C# and drop the data into a list and then dump the data to a SQL Database database... The class im interested using is using CsvHelper;

This is two sample data in the CSV File:

XML
1,2013-05-14,NISSAN,059 7013,1995,VAN,Deisel,NISSAN,0000,NISSAN,Red,MARADANA,op1,description,p1.jpg+p2.jpg+p3.jpg,r1+r2+r3

2,2013-05-14,NISSAN2,059-7013,1995,VAN,Deisel,NISSAN,0000,NISSAN,Red,MARADANA,op1,description,p1.jpg+p2.jpg+p3.jpg,r1+r2+r3

This is the class for the CSV file

C#
class Vehicle
    {
        public int ID { get; set; }
        public string ClosingDate { get; set; }
        public string Model { get; set; }
        public string Reg { get; set; }
        public string YearOfManufacture { get; set; }
        public string VehicleType { get; set; }
        public string Fuel { get; set; }
        public string Brand { get; set; }
        public string Mileage { get; set; }
        public string Make { get; set; }
        public string Colour { get; set; }
        public string VehicleYard { get; set; }
        public string BasicOptions { get; set; }
        public string Description { get; set; }
        public List<string> Photos { get; set; }
        public List<string> Regions { get; set; }
    }


I tried and Read the CSV file using this code:

C#
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            CsvHelperRead();

            Console.Read();
        }

        private static void CsvHelperRead()
        {

            CsvConfiguration config = new CsvConfiguration();
            config.HasHeaderRecord = false;

            using (var reader = new CsvReader(new StreamReader(@"C:\Users\afshandc\Desktop\0729\vTest.csv"), config))
            {
                while (reader.Read())
                {
                    Console.WriteLine(reader.GetField(1));
                }
            }
        }
    }
}


I Need to Read the CSV File and Drop it to this list, Any HElp Help Will be appriciated!Thanks!

C#
class CSV
   {
       public List<Vehicle> ReadVechileCSV()
       {







       }
   }
Posted
Comments
[no name] 29-Jul-13 5:11am    
So read the documentation for this "CsvHelper" and/or ask the author how to use his software.
Hawkeye101 30-Jul-13 1:38am    
@SAKryukov Any Help??

1 solution

As far as I understand it you got to the point where you read the file and showed the content in a messagbox but don't populate a new object? If thats the case you just needs to add som code to you third section:


C#
List<vehicle> lv = new List<vehicle>();

while (reader.Read())
 {

    Vehicle v = new Vehicle();

 //Console.WriteLine(reader.GetField(1));
     v.ID = reader.GetField(1);
     v.ClosingDate  = Convert.ToDateTime(reader.GetField(2));
     ....

lv.add(v);

 }</vehicle></vehicle>



But I might not have understod your question....
 
Share this answer
 
Comments
Hawkeye101 29-Jul-13 5:36am    
There wasnt any other way to interpret this question! Anyway ill try this! Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900