1
temp= {
       "Vikas": 1,
       "Pravin": 2,
       "Akshay": 5,
       "Vijay": 3,
       "Prasad": 4 
      }

This is value I am getting in temp variable I want to store this data in database with one additional column like following

name      rank   createdby
vikas      1       nitin
pravin     2       nitin
akshay     5       nitin
vijay      3       nitin
Prasad     4       nitin


Note:  name column is employee name so it is anything 

I tried like getting in C#

[System.Web.Services.WebMethod]
public static string setAnswer(string jsondata)
{
    List<string[]> data = JsonConvert.DeserializeObject<List<string[]>>(jsondata);
}

Question.

1) how to get this json data in C# and add additional column

2) Is it possible to pass whole data using multiple row insert in one query instead of using loop and insert single row.

User
  • 1,334
  • 5
  • 29
  • 61
  • 4
    What type db access are you using? EF ? ADO? NHibernate? – BWA Feb 06 '17 at 10:58
  • i am using sql server 2008 – User Feb 06 '17 at 10:59
  • 1
    depends on the database you are using. If the db allows bulkinsert its definitly possible. to add a row to an array you have to copy the whole array. – Sebastian L Feb 06 '17 at 11:00
  • 3
    upgrade to sql server 2016 – anatol Feb 06 '17 at 11:00
  • it is possible or not using sql server 2008 and how to do my it first question – User Feb 06 '17 at 11:01
  • 1
    yes to avoid for loop you can use table valued parameters you need to convert list to datatable check this link https://blog.sqlauthority.com/2008/08/31/sql-server-table-valued-parameters-in-sql-server-2008/ let me know if you require more help – Chintan Udeshi Feb 06 '17 at 11:02
  • @Chintan Udeshi please give example how to pass value for such procedure using C# – User Feb 06 '17 at 11:08
  • 2
    I assume `temp = ` is your way of saying that you have a variable with that content, not that this is part of the payload/string because this part is not legal json. – Lasse V. Karlsen Feb 06 '17 at 11:10
  • 1
    @Pravin check this link http://stackoverflow.com/questions/7097079/c-sharp-sql-server-passing-a-list-to-a-stored-procedure – Chintan Udeshi Feb 06 '17 at 11:27

3 Answers3

3

Edited after explantions:

You can deserialize it to dictionary in this way:

var data = JsonConvert.DeserializeObject<Dictionary<string, int>>(temp);

I this point we have dictionary with data. How to save it into DB depends how you access to db.

BWA
  • 5,672
  • 7
  • 34
  • 45
3

The correct way to deserialize this content:

{
    "Vikas": 1,
    "Pravin": 2,
    "Akshay": 5,
    "Vijay": 3,
    "Prasad": 4 
}

Is to deserialize it into a Dictionary<string, int>.

Here's a LINQPad example:

void Main()
{
    const string json = @"{
        ""Vikas"": 1,
        ""Pravin"": 2,
        ""Akshay"": 5,
        ""Vijay"": 3,
        ""Prasad"": 4
    }";
    Dictionary<string, int> empsAndNumbers =
        JsonConvert.DeserializeObject<Dictionary<string, int>>(json);
    empsAndNumbers.Dump();
}

which outputs:

example output

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
2

Create an Entity : MemberRank

public class MemberRank
{
    public int Id { get; set; }
    public string MemberName { get; set; }
    public int Rank{ get; set; }
    public string CreatedBy { get; set; }
}

Then in Controller,

var membersText = File.ReadAllText(Path.Combine(envContentRootPath, @"PathOfTheFile"));
var members = JsonConvert.DeserializeObject<List<MemberRank>>(membersText);
members.ForEach(cb => cb.CreatedBy = "nitin");
_dbContext.Set<MemberRank>().AddRange(members);
_dbContext.SaveChanges();

For this to work, you need to change your json file as follows:

[
  {
     "memberName" : "Vikas",
     "rank" : 1
  },
  {
     "memberName" : "Pravin",
     "rank" : 2
  },
  {
     "memberName" : "Akshay",
     "rank" : 5
  },
  {
     "memberName" : "Vijay",
     "rank" : 3
  },
  {
     "memberName" : "Prasad",
     "rank" : 4
  }
]
Dronacharya
  • 1,191
  • 11
  • 13