1

What is the best way to save JSON objects to a SQL Server database? How can I save the deserialized objects to a database instead of outputting to the console? Maybe I could use Entity Framework? Hope somebody can put me on the right track. Thanks

public class Program
{
    static void Main(string[] args)
    {
        string json = @"{
       'Email': '[email protected]',
       'Active': true,
       'CreatedDate': '2015-01-20T00:00:00Z'}";

        Account account = JsonConvert.DeserializeObject<Account>(json);

        Console.WriteLine(account.Email);
        Console.WriteLine(account.CreatedDate);
        Console.ReadKey();
    }
}

Account.cs

public class Account
{
    public string Email { get; set; }
    public DateTime CreatedDate { get; set; }
}
2
  • 1
    You want to save the Deserialized object to database? Or the Serialized string? Create a insert statement, use the property values from deserialized object and insert into db. Commented Feb 26, 2016 at 10:33
  • Deserialized object to database Commented Feb 26, 2016 at 11:01

1 Answer 1

4
public class Program
{
    static void Main(string[] args)
    {
        string json = @"{
       'Email': '[email protected]',
       'Active': true,
       'CreatedDate': '2015-01-20T00:00:00Z'}";

        Account account = JsonConvert.DeserializeObject<Account>(json);

        string email=account.Email.ToString();
        DateTime date=account.CreatedDate.ToDateTime();

      //Open a connection with database.Write a insert query and provide these values and run the query.
    }
}
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.