将JSON对象保存到SQL Server数据库 [英] Saving JSON objects to SQL Server database

查看:493
本文介绍了将JSON对象保存到SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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': '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

Account.cs

public class Account
{
    public string Email { get; set; }
    public DateTime CreatedDate { get; set; }
}

推荐答案

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

这篇关于将JSON对象保存到SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆