0

at first I am a novice, I am learning is that only two months. (Sorry for my english, I hope u will understand.) Problem is: I am trying to create a small database with console application. I have student.csv where are all information about students. When I start application, all informations from this .csv will save into Lists. Like this:

 List<Student> zoznam = new List<Student>(); List<string> inicZac = new List<string>(); List<string> ID = new List<string>(); List<string> Meno = new List<string>(); List<string> Priezvisko = new List<string>(); List<string> Adresa = new List<string>(); List<string> DatumNarodenia = new List<string>(); List<string> Heslo = new List<string>(); List<string> Login = new List<string>(); List<string> inicKon = new List<string>(); private int id; StreamReader reader = new StreamReader(File.OpenRead("student.csv")); public databazaStudentov() { while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(';'); inicZac.Add(values[0]); ID.Add(values[1]); Meno.Add(values[2]); Priezvisko.Add(values[3]); Adresa.Add(values[4]); DatumNarodenia.Add(values[5]); Heslo.Add(values[6]); Login.Add(values[7]); inicKon.Add(values[8]); zoznam.Add(new Student(values[1], values[2], values[3], values[4], values[5], values[6], values[7])); } } 

It works good. But now I want secure an easy write into this .csv when I create a new student in a running application.This function is creating a new student:

 public void addStudent(string meno, string priezvisko, string adresa, string datum) { string tempID = generujID(); //random gener ID string tempLogin = generujLogin(meno, priezvisko); //random gener Login string tempHeslo = generujHeslo(); //random gener password zoznam.Add(new Student(tempID, meno, priezvisko, adresa, datum, tempLogin, tempHeslo)); ID.Add(tempID); Meno.Add(meno); Priezvisko.Add(priezvisko); Adresa.Add(adresa); DatumNarodenia.Add(datum); Login.Add(tempLogin); Heslo.Add(tempHeslo); // I created a new student and now I want save him into the csv } 

and here is my .csv: https://i.sstatic.net/iyGCy.jpg

There are probably more ways how to fix it. I will be gratefull if someone show me How to save a new student on a new row or How to overwrite with Lists everything in my student.csv . Thanks for tips and sorry for my english.

2
  • string.Join is the counterpart to string.Split Commented Apr 11, 2015 at 14:05
  • 1
    just curious why csv as storage format? I'd go with something like JSON that can easily be serialized and deserialized into C# object and has better support for hierarchical data structure just in case. And since you are using file system anyhow, you can just persist and read it as a whole. Flow is something like: load json from storage, deserialize into C# object, make changes in C# object like remove from list or add to list, etc., serialize back to json and write to storage. Commented Apr 11, 2015 at 14:09

3 Answers 3

2

I would create a new class to create csv file from your student.

public class CsvGen { private _fileName=string.Empty; private StringBuilder csvRows; public CsvGen(string fileName) { _fileName=fileName; csvRows=new StringBuilder(); } public void Add(Student student) { var row=String.Format("{0},{1},{2},{3}",student.Id,student.Name,student.City, Environment.NewLine); csvRows.Append(row); } public void SaveFile() { System.IO.File.WriteAllText(_fileName,csvRows.ToString()); } } 

Now from your other class, you can create a new instance of this class and call the AddStudent method everytime you add an item to the list and call SaveFile after your loop.

CsvGen csvGen=new CsvGen(@"C:\\temp\myFile.csv"); // ^ any location with proper permission for .net to write files. public void AddStudent(int id, string name, string city) { Student stud=new Student(); stud.Id=id; stud.Name=name; stud.City=city; csvGen.Add(stud); } 

So once you create all students, You may call the SaveFile method to save the csv file

while (!someCondition) { AddStudent(2,"scott","detroit"); } csvGen.SaveFile(); 
Sign up to request clarification or add additional context in comments.

Comments

1

A quick solution for your question.

public void addStudent(string meno, string priezvisko, string adresa, string datum) { //your code //......... // I created a new student and now I want save him into the csv using(StreamWriter sw = new StreamWriter("student.csv", true))//true to append after the file { sw.WriteLine("{0};{1};{2};{3};{4};{5};{6};", tempID, meno, priezvisko, adresa, datum, tempLogin, tempHeslo); sw.Close(); } } 

Comments

0

If you want to read and write csv, then I'd recommend you try the CsvHelper library, as it would be much simpler than the way you're doing it currently.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.