user2342643
user2342643

Reputation: 35

Saving JSON objects to SQL Server database

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; }
}

Upvotes: 1

Views: 5394

Answers (1)

VVN
VVN

Reputation: 1610

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.
    }
}

Upvotes: 4

Related Questions