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();
Show part of your code that you are using to import and we will tell you how to change it to achieve your goal.
– Caffé
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... ?
– Daniel
lineFile = file.Readline(); fields = lineFile.Split(new string[] { ";" }, Stringsplitoptions.None); record = dt.Newrow();
– Daniel
He’s reading all the lines
– Daniel
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.– Caffé
Yes it is inside that while (!file.Endofstream), beauty I will try it now !
– Daniel
I posted a reply suggesting a way to make this IF.
– Caffé
right thanks ! my loop I keep then ne ? while (!file.Endofstream)
– Daniel
Caffe is that I managed to disregard the header thanks a lot for the help !
– Daniel
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.
– Caffé
Right you can leave , and I’ve accepted the answer, it’s my first time here thanks
– Daniel
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.– Beterraba
So the problem is that it exceeds the character limits
– Daniel
length exceeded by 2637 characters
– Daniel
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();
– Daniel
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
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é
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 ?
– Daniel