Disregard the header of a . csv file when importing into the database

Asked

Viewed 934 times

1

I need to read a file . csv disregard the header when importing to the database as is possible ?

I’m using the following code:

linhaArquivo = arquivo.ReadLine(); 

campos = linhaArquivo.Split(new string[] { ";" }, 
    StringSplitOptions.None);

registro = dt.NewRow();
  • 1

    Show part of your code that you are using to import and we will tell you how to change it to achieve your goal.

  • So Gustavo in my code he’s reading the whole file, the first line that’s the header too, I’m not getting him to read the first line... ?

  • lineFile = file.Readline(); fields = lineFile.Split(new string[] { ";" }, Stringsplitoptions.None); record = dt.Newrow();

  • He’s reading all the lines

  • This code must be inside a loop that iterates between the lines of the file. Just ignore the first iteration (by adding an if below linhaArquivo = arquivo.ReadLine();). I suggest debugging code line by line to understand how it works.

  • Yes it is inside that while (!file.Endofstream), beauty I will try it now !

  • I posted a reply suggesting a way to make this IF.

  • right thanks ! my loop I keep then ne ? while (!file.Endofstream)

  • Caffe is that I managed to disregard the header thanks a lot for the help !

  • That’s nice, Daniel. Next time just go ahead and show the code and consider accepting the answer if you solved your problem - all this makes this tool here a nicer place.

  • Right you can leave , and I’ve accepted the answer, it’s my first time here thanks

  • Daniel, to complement, you could include your "full" code (with the while) in the question, so that it becomes clearer to the next ones who have the same doubt as you.

  • So the problem is that it exceeds the character limits

  • length exceeded by 2637 characters

  • using (Streamreader file = new Streamreader(path)) { string lineArchive; string[] fields; Datarow record; bool headerJaLido = false; while (!.Endofstream file) { lineArchive = file.Readline(); if (!headerJaLido) { headerJaLido = true; ;continue; } fields = fileLine.Split(new string[] { ";" }, Stringsplitoptions.None); record = dt.Newrow();

  • record["Name"] = fields[0]. Tostring(). Trim(); record["Document"] = fields[1]. Trim(). Tostring(); record["Address"] = fields[3]. Trim(). Tostring(); record["Complement"] = fields[5]. Trim(). Tostring(); record["Neighborhood"] = fields[6]. Trim(). Tostring(); registro["Cidade"] = campos[8]. Trim(). Tostring(); registro["CEP"] = campos[9]. Trim(). Tostring(); record["Number"] = Convert.Toint32(fields[4]); record["Typename"] = Convert.Toint32(fields[2]); record["Status"] = Convert.Toint32(fields[7]); dt.Rows.Add(record); } Redt turn;

  • Daniel, I saw your new comments by accident. It is no use asking for more help in the comments of this question. It seems to me that the problem you had when you posted this question was solved. Now create a new question with your new problems, showing all the relevant code, the line that gives the error and the complete error message.

  • Caffé he was solved even ta all right... I was talking to Beet who asked me to post the whole code but it exceeds the limit of characters understood ?

Show 13 more comments

1 answer

2


This code must be inside a loop that iterates between the lines of the file.

Just ignore the first iteration by adding an if below linhaArquivo = arquivo.ReadLine():

bool cabecalhoJaLido = false;

for ... // você não postou o código que mostra o loop.

    linhaArquivo = arquivo.ReadLine(); 

    if (!cabecalhoJaLido) {
        cabecalhoJaLido = true;
        continue;
    }

    campos = linhaArquivo.Split(new string[] { ";" }, 
        StringSplitOptions.None);
  • More efficient read the header before the for :)

  • @Beet As efficient as. Just fewer lines. Not an IF bool that will make a difference in efficiency :-) But I agree it would be better. It’s been a long time since I’ve programmed, and I don’t even have any IDE here. Kind of rusty...

  • @Beet Anyway, when reading the header before entering the loop, you would have to find a way to do it without relying on a comment like arquivo.ReadLine(); // ignora o cabeçalho. I defend that the code has to be self-explanatory and this one despite more lines, it is very clear :-)

  • But comments are part of hahaha code! It was just to point out the possibility of ignoring the line before the for. It’s how I would do and found it interesting to share.

  • @Beet Then I totally disagree. Comment is only part of the code that did not succeed in being a good code.

  • I prefer a comment to a if unnecessary. Although his statement has its merits, I disagree with the excessive generalization. But, again, just a matter of taste. I don’t want to polemicize!

  • @Beet Yeah, this subject is always a long discussion; which I like very much, including :D But in fact here is not the right place.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.