0

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.

2
  • 1
    have you looked at the following Json to C# Class Converter from there you could get the data into a Class and from there into DataTables etc.. or[ convert the json to CSV](konklone.io/json) Commented Jan 25, 2016 at 22:23
  • 1
    What is the problem exactly? If you have a known database structure, then deserializing the JSON string using a POCO object created by (for example) EF will work. Commented Jan 25, 2016 at 22:28

1 Answer 1

3
  1. Deserialize the JSON string into an array of dictionaries.

  2. Loop through the array.

  3. Using the dictionary keys, create an SQL statement of the form: INSERT INTO myTable (key1, key2, ...) VALUES (@key1, @key2, ...).

  4. Looping through the dictionary's key-value-pairs, add the values as parameters: AddWithValue("@" + key, value).

  5. Execute the SQL statement.

  6. Profit.

The implementation is left as an exercise.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.