将JSON对象保存到SQL Server数据库

时间:2016-02-26 10:32:40

标签: c# sql-server json

将JSON对象保存到SQL Server数据库的最佳方法是什么?如何将反序列化的对象保存到数据库而不是输出到控制台?也许我可以使用实体框架?希望有人能让我走上正轨。感谢

public class Program
{
    static void Main(string[] args)
    {
        string json = @"{
       'Email': 'Joebloggs@example.com',
       '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; }
}

1 个答案:

答案 0 :(得分:3)

public class Program
{
    static void Main(string[] args)
    {
        string json = @"{
       'Email': 'Joebloggs@example.com',
       '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.
    }
}