Daryl Gill
Daryl Gill

Reputation: 5524

Using a foreach to split elements into their own vairable

I'm currently working on a method to import a CSV into a database (custom method to also get a little more knowledge on C#) I've encountered the following problem.

I'm trying to insert a split string into a database, each value being inserted into a different column.

I currently have:

ID | ClientName | ShopName | Address | PostCode | ContactNumber

as my columns. ID being the identify and self increment

and my code is:

public void FileContents(string Str)
       // private System.Collections.Generic.List<Tuple<string,string,string,string,string>> FileContents(string Str)
        {
            int Counter = 0;
            //string QueryString = "INSERT INTO "
            string[] split = Str.Split(',');
            foreach (string Element in split)
            {
                Element.Trim(new Char[] { ' ', '\t' });
                Console.WriteLine(Element);

                Counter++;
            }

            Console.ReadKey(true);


        } 

Using the foreach loop seems to yield the correct resuls which look like:

Mrs DummyName

ShopName Place Holder

Address Place Holder

PostCode place holder

ContactNumber Place Holder

which is as expected. The approach on how to insert each of those elements into the correct column eludes me. I considered using:

Console.WriteLine(split[0]);

But this does not yield exptected results. Expected being:

ClientName Placeholder

ClientName Placeholder

But instead yields:

ClientName Placeholder #1

ClientName Placeholder #2

ClientName Placeholder #3

Address Placeholder of ClientName placeholder 3

so it is not iterating through each of the client names without skipping to the address.

So, what would be the best way to get each of the elements in the foreach loop to then insert into the database?

Upvotes: 1

Views: 1706

Answers (1)

Christian Phillips
Christian Phillips

Reputation: 18769

Since a string is immutable, this line will not alter Element...

Element.Trim(new Char[] { ' ', '\t' });

You would need something like:

var newElement = Element.Trim(new Char[] { ' ', '\t' });
Console.WriteLine(newElement); 

Without seeing the input, I'll assume you have a file...

var lines = new List<string>();

  lines.AddRange(File.ReadAllLines(@"test.txt"));
  foreach(var lineToSplit in lines)
  {
      string[] split = lineToSplit.Split(',');
      foreach (string element in split)
         {
            var curElement = element.Trim(new Char[] { ' ', '\t' });
            Console.WriteLine(curElement);
         }
  }

You could create a class

public class Clients
 {
   public string ClientName { get; set; }
   public string ShopName { get; set; }
   public string Address { get; set; }
   public string PostCode { get; set; }
   public string ContactNumber { get; set; }            
 }

and then populate a list of clients...

lines.AddRange(File.ReadAllLines(@"test.txt"));
var clients = new List<Clients>();

foreach (var lineToSplit in lines)
{
    var split = lineToSplit.Split(',');
    clients.Add(new Clients
    {
        ClientName = split[0]
        //etc
    });
}

Upvotes: 1

Related Questions