Issue with RESTful webservice +JSON+SQL stored procedure project

后端 未结 1 1011
故里飘歌
故里飘歌 2021-01-25 21:45

I know for a fact that there is something I miss. My whole project is somewhat copy&paste of various \"how to...\"s, my knowledge of C# is basic at best and I need to have i

1条回答
  •  -上瘾入骨i
    2021-01-25 22:10

    OK, so there are several things that you need to change to make it working:

    1. Add parameterless constructor to Note as it will be needed for deserialization:

      public Note()
      {
      }
      
    2. Get rid of "static" in Note's fields:

      public static string Client { get; set; }

      public static int Case { get; set; }

      public static string Text { get; set; }

      public static int NoteId { get; set; }

      public static string R1 { get; set; }

      public static string R2 { get; set; }

      public static string S1 { get; set; }

      public static DateTime Date { get; set; }

      public static bool Type { get; set; }

    3. Don't send JSON array if you want just 1 object, it won't deserialize. You are expecting single object, not array, so don't send array.

    4. You have Type as bool, but you are sending string "1", this will not deserialize to true value as you might expected. Either send true/false (not "true"/"false") or change type of Type to string.

    5. Get rid of that private item field, you don't need it:

      private Note item;

    6. Get rid of those constructors that you have there

      public Note(string json)

      public Note(Note item)

      Not only that they make no sense and won't work, you don't need them as JSON deserializer will fill the fields for you.

    EDIT: For example, you say it does not build because there is no more a constructor with one parameter. Of course it does not build, there is this line

    Note notesdata = new Note(item);
    

    but you do not need that line. What is the idea behind this line? You want an instance of Note class, but you already have it in "item" variable. You do not need to create a second copy of that. So get rid of this too.

    Another reason, why it won't compile is that you get rid of those static fields, while you still have this in your Add method:

            cmd.Parameters.Add("@Text", SqlDbType.VarChar).Value = Note.Text.Trim();
            cmd.Parameters.Add("@When", SqlDbType.DateTime).Value = Note.Date;
    

    and I am quite sure you do not want that. Instead, you want to use the instance of the object that were sent to you:

            cmd.Parameters.Add("@Text", SqlDbType.VarChar).Value = item.Text.Trim();
            cmd.Parameters.Add("@When", SqlDbType.DateTime).Value = item.Date;
    

    Another thing is that there is usually no reason why would Add method return the object being added to DB. So feel free to change this

       public Note Add(Note item)
    

    to this

       public void Add(Note item)
    

    and do not return anything, you do not need it.

    I am no expert on SqlConnection and things around it, so that part I do not comment. I use EF in my projects for working with DB. So there might be some problems in that part, but I can't comment on that.

    0 讨论(0)
提交回复
热议问题