0

I'm using c# MVC project. I have a customer class and customer table.I have a Insert function with four parameter name , surname ,phone,address.I want to read .txt file line by line and split with "," and use this Insert function but I don't know how can create algorithm.

 static void AddCustomer(string Name, string Surname, string Phone, string Address)
    {
        using (var session = NHibernateHelper.OpenSession())
        {
            using (var transaction = session.BeginTransaction())
            {
                var customer = new Customer
                {
                    Name = Name,
                    Surname = Surname,
                    Phone = Phone,
                    Address = Address,
                };
                session.Save(customer);
                transaction.Commit();
            }
        }
    }



            while ((line = file.ReadLine()) != null)
            {
                string text = file.ReadToEnd();
                string[] lines = text.Split(',');
                for (int i = 0; i < lines.Length; i++)
                {
                    //HOW CAN I USER ADDCUSTOMER()
                }  

                counter++;
            }
TFrost
  • 769
  • 2
  • 12
  • 31
MrtDev
  • 3
  • 7

2 Answers2

2

You've almost got it. Assuming file is a StreamReader, you can just split the current line on comma, and pass the separate parts to the AddCustomer method:

while ((line = file.ReadLine()) != null)
{
    // Split the line on comma
    var lineParts = line.Split(',');

    string name = lineParts[0];
    string surname = lineParts[1];
    string phone = lineParts[2];
    string address = lineParts[3];

    AddCustomer(name, surname, phone, address);
}

Please note that this does no error checking at all (lineParts[1] will blow up if there's no comma in the given line) and that this is a bad way to parse CSV (if the data contains comma's, which addresses tend to do, it'll not work properly). Use a CSV parsing library.

See Parsing CSV files in C#, with header and plenty of other questions about CSV, where it is suggested that you use the FileHelpers library. Your class that maps to and from the CSV file will look like this:

[DelimitedRecord(",")]
[IgnoreEmptyLines()]
public class MyProduct
{
    [FieldOrder(0)]
    public string Name { get; set; }

    [FieldOrder(1)]
    public string Surname { get; set; }

    [FieldOrder(2)]
    public string Phone { get; set; }

    [FieldOrder(3)]
    public string Address { get; set; }
}

And the code to read the file:

var engine = new FileHelperEngine<CustomerCsvRecord>();
CustomerCsvRecord[] customers = engine.ReadFile(fileName);

foreach (var customer in customers)
{
    AddCustomer(customer.Name, customer.Surname, customer.Phone, customer.Address);
}
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • +1: I've always enjoyed reading your answers. One thing though, can you not do something like this? `var engine = new FileHelperEngine(); var customers = engine.ReadFile(fileName);` [MSDN reference](http://www.filehelpers.net/docs/html/T_FileHelpers_FileHelperEngine_1.htm). – John H Sep 06 '15 at 10:05
  • Not a problem. :) Actually, the `var` is fine. It's the cast I don't think is necessary. – John H Sep 06 '15 at 10:07
  • 1
    @JohnH thanks, those comments make it worth the effort. ;) I have changed the code to use the generic `FileHelperEngine`, that indeed makes the code somewhat more readable. – CodeCaster Sep 06 '15 at 10:09
0

This will do your job.

        string fileContent = System.IO.File.ReadAllText("YOUR_FILE_PATH");

        //assumeing each customer record will be on separate line
        string[] lines = fileContent.Split(new string [] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);

        foreach (string line in lines)
        {
            //assuming a single line content will be like this "name,surname,phone,address"
            string[] items = line.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            Customer cust = new Customer();
            cust.Name = items[0];
            cust.Surname = items[1];
            cust.Phone = items[2];
            cust.Address = items[3];

            //now use this 'cust' object
        }
M_Idrees
  • 2,080
  • 2
  • 23
  • 53