I asked a question before about how I can do a simple insert/update from a CSV file into a database. I was given some code that I have adopted here. The recordList is the parsed CSV file as a collection:
SqlDataAdapter dataAdpater = new SqlDataAdapter(
"SELECT * FROM Cats WHERE UniqueCatName = @UniqueCatName", "data source=localhost;initial catalog=Kitties;integrated security=True;MultipleActiveResultSets=True;");
DataTable testTable = new DataTable();
dataAdpater.Update(testTable);
foreach (var record in recordList)
{
dataAdpater.SelectCommand.Parameters.AddWithValue("@UniqueCatName", record.UniqueCatName);
int rowsAdded = dataAdpater.Fill(testTable);
if (rowsAdded == 0)
{
testTable.Rows.Add(
record.UniqueCatName,
record.Forename,
record.Surname
);
}
else
{
}
}
dataAdpater.Update(testTable);
I'm kinda going in blind, I've looked at a ton of tutorials but I couldn't find one that clearly demonstrated how to use the SqlDataAdapter for both adding and updating.
From what I read, you have to specify an update command and an insert command? So I'm not -entirely- sure what's going on with the code above, but I guess it is able to do a simple insert without me giving extra instructions.
I couldn't figure out what to put in the 'else' bit though that the answerer to my previous question gave. I need to retrieve and update the particular row, but I don't know how to do that. Any ideas?