I want to import data from CSV file to SQL database using Entity Framework

后端 未结 2 533
刺人心
刺人心 2020-12-22 10:50

I want insert data from CSV file to SQL table. Here is my code I don\'t know how to go further

var readcsv = File.ReadAllText(filepath);
string[] csvfilereco         


        
2条回答
  •  礼貌的吻别
    2020-12-22 11:40

    You need to know which column of the CSV maps to which property. With this information you can map values into properties.

    Assuming you have a dictionary of indexes columnMap you can use

    var cells = row.Split(',');
    var cards = new Cards {
      prop = cells[columnMap["prop"]],
      nextProp = cells[columnMap["nextProp"]],
      …
    }
    

    Note I do not iterate over the separate values from one row in the CSV.

    Also note you need a proper CSV parser to handle the escaping/quoting necessary for when values contain commas or quotes.

提交回复
热议问题