Reputation: 35
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
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