Reputation: 3264
I've been looking everywhere, can't seem to find a solution. I have a very long string in a textfile. It's already in a Json format i.e.
[{"column":"data", "anothercolumn":"data"}, {"column":"data", "anothercolumn": "data"}]
I want to convert this to an INSERT MS SQL statement using C#. Is there any way to do this? I can't figure it out myself using DeserializeObject or DataSets and DataTables. Help is much appreciated.
Upvotes: 0
Views: 878
Reputation: 172200
Deserialize the JSON string into an array of dictionaries.
Loop through the array.
Using the dictionary keys, create an SQL statement of the form: INSERT INTO myTable (key1, key2, ...) VALUES (@key1, @key2, ...)
.
Looping through the dictionary's key-value-pairs, add the values as parameters: AddWithValue("@" + key, value)
.
Execute the SQL statement.
Profit.
The implementation is left as an exercise.
Upvotes: 3