2016-02-26 61 views
0

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

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

你要保存的'Deserialized'对象数据库?或者'Serialized'字符串?创建一个插入语句,使用来自反序列化对象的属性值并插入到数据库中。 –

+0

反序列化对象到数据库 – user2342643

回答

3
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. 
    } 
} 
相关问题