2016-02-26 62 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. 
    } 
} 
相關問題