ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 254.5k

How to create function return statement insert into table v

Aug 12 2019 10:00 PM
Problem
How to return statement insert into table values from json file ?
I work on newton soft library and I try to insert data to table master_table
but my problem how to make function return insert statement as bellow :
  1. insert into master_table(id,branch_id,name,address,phone) values (1,1,"bar","fleet street","555")  
table ,keys,fields content flexible or dynamic .
my jsonfile D:\\1.json as below :
  1. {  
  2. "master" : {  
  3. "table""master_table",  
  4. "fields": {  
  5. "name" : "bar",  
  6. "address" : "fleet street",  
  7. "phone" : "555"  
  8. },  
  9. "keys":{  
  10. "id" : 1,  
  11. "branch_id" : 1  
  12. }  
  13. }  
  14. }  
I already make get keys and fields but cannot make concatenate
insert statement as result .
How to concatenate insert statement that have keys + fields
as statement on first thread .
 
  1. public static ExpandoObject ToExpando(string json)  
  2. {  
  3. if (string.IsNullOrEmpty(json))  
  4. return null;  
  5. return (ExpandoObject)ToExpandoObject(JToken.Parse(json));  
  6. }  
  7.   
  8.   
  9. private static object ToExpandoObject(JToken token)  
  10. {  
  11.   
  12. switch (token.Type)  
  13. {  
  14. case JTokenType.Object:  
  15. var expando = new ExpandoObject();  
  16. var expandoDic = (IDictionary<stringobject>)expando;  
  17. foreach (var prop in token.Children<JProperty>())  
  18. expandoDic.Add(prop.Name, ToExpandoObject(prop.Value));  
  19. return expando;  
  20. case JTokenType.Array:  
  21. return token.Select(ToExpandoObject).ToList();  
  22.   
  23. default:  
  24. return ((JValue)token).Value;  
  25. }  
  26. }  
  27. static void Main(string[] args)  
  28. {  
  29. string JsonData = File.ReadAllText("D:\\1.json");  
  30. var ebj = SqlFactory.ToExpando (JsonData);  
  31. var name = (ebj as dynamic).master.table;  
  32. var fields = (ebj as dynamic).master.fields;  
  33. foreach (dynamic i in fields)  
  34. {  
  35. string key = i.Key;  
  36. object value = i.Value;  
  37. }  
  38. var keys = (ebj as dynamic).master.keys;  
  39.   
  40. }  

Answers (4)