Iam trying to get a JSON array into a MSSQL table using linq. I almost got it to work but with the current code, it inserts the first row for the duration of the for at the list part. the table looks like this after the insert. This is my code:
List<insertmultiapiwhitelist> thelist = new List<insertmultiapiwhitelist>();
string json = "[{'naam':'Default Web Site','webid':1,'verbruik':0,'datum':'2016-12-05 00:00:00'}]";
//json above has 150 items, purged to keep it clean.
dynamic jsondata = JsonConvert.DeserializeObject(json);
foreach (var jsonitem in jsondata)
{
thelist.Add(new insertmultiapiwhitelist()
{
naam = jsondata[0].naam,
webid = jsondata[1].webid,
verbruik = jsondata[2].verbruik,
datum = jsondata[3].datum
});
}
foreach(var listgebruik in thelist)
{
webfarm_data dbtoevoegingen = new webfarm_data();
dbtoevoegingen.naam = listgebruik.naam;
dbtoevoegingen.webid = listgebruik.webid;
dbtoevoegingen.verbruik = listgebruik.verbruik;
dbtoevoegingen.datum = DateTime.Parse(listgebruik.datum);
DeDB.webfarm_data.Add(dbtoevoegingen);
}
var updateantwoord = new insertmultiapiwhitelist { antwoord = "Niets ontvangen" };
try
{
DeDB.SaveChanges();
updateantwoord = new insertmultiapiwhitelist { antwoord = "Insert voltooid" };
}
catch (Exception e)
{
var foutmelding = e.ToString();
updateantwoord = new insertmultiapiwhitelist { antwoord = foutmelding };
}
return View(updateantwoord);