Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. storing data

storing data

Scheduled Pinned Locked Moved C#
mysql
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    damianrda
    wrote on last edited by
    #1

    Hey, just wondering what the best method would be for storing simple objects in an mysql table. public class Address { public int Number { get; set; } public int Unit { get; set; } public string Street { get; set; } public string City { get; set; } public string Province { get; set; } public string Country { get; set; } public string PostalCode { get; set; } }

    T L D 3 Replies Last reply
    0
    • D damianrda

      Hey, just wondering what the best method would be for storing simple objects in an mysql table. public class Address { public int Number { get; set; } public int Unit { get; set; } public string Street { get; set; } public string City { get; set; } public string Province { get; set; } public string Country { get; set; } public string PostalCode { get; set; } }

      T Offline
      T Offline
      TheFM234
      wrote on last edited by
      #2

      You could serialize the object and store the xml.

      1 Reply Last reply
      0
      • D damianrda

        Hey, just wondering what the best method would be for storing simple objects in an mysql table. public class Address { public int Number { get; set; } public int Unit { get; set; } public string Street { get; set; } public string City { get; set; } public string Province { get; set; } public string Country { get; set; } public string PostalCode { get; set; } }

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        damianrda wrote:

        Hey, just wondering what the best method would be for storing simple objects in an mysql table.

        One popular method is called Object Relational Mapping[^]

        led mike

        1 Reply Last reply
        0
        • D damianrda

          Hey, just wondering what the best method would be for storing simple objects in an mysql table. public class Address { public int Number { get; set; } public int Unit { get; set; } public string Street { get; set; } public string City { get; set; } public string Province { get; set; } public string Country { get; set; } public string PostalCode { get; set; } }

          D Offline
          D Offline
          damianrda
          wrote on last edited by
          #4

          Ok, well i managed to serialize the object using a binaryformatter and then base64'd the result. public string Serialize() { MemoryStream MemStream = new MemoryStream(); BinaryFormatter Serializer = new BinaryFormatter(); Serializer.Serialize(MemStream, this); return Encoding.ASCII.GetString(MemStream.ToArray()).ToBase64(); } but im having some issues when deserializing: "End of stream encountered before parsing was completed." public static Address Deserialize(string SerializedData) { Address Address; MemoryStream MemStream = new MemoryStream(); BinaryFormatter Serializer = new BinaryFormatter(); string Decoded = SerializedData.FromBase64(); byte[] Buffer = Encoding.ASCII.GetBytes(Decoded); MemStream.Write(Buffer, 0, Buffer.Length); Address = (Address)Serializer.Deserialize(MemStream); <-- fails right here... return Address; }

          M 1 Reply Last reply
          0
          • D damianrda

            Ok, well i managed to serialize the object using a binaryformatter and then base64'd the result. public string Serialize() { MemoryStream MemStream = new MemoryStream(); BinaryFormatter Serializer = new BinaryFormatter(); Serializer.Serialize(MemStream, this); return Encoding.ASCII.GetString(MemStream.ToArray()).ToBase64(); } but im having some issues when deserializing: "End of stream encountered before parsing was completed." public static Address Deserialize(string SerializedData) { Address Address; MemoryStream MemStream = new MemoryStream(); BinaryFormatter Serializer = new BinaryFormatter(); string Decoded = SerializedData.FromBase64(); byte[] Buffer = Encoding.ASCII.GetBytes(Decoded); MemStream.Write(Buffer, 0, Buffer.Length); Address = (Address)Serializer.Deserialize(MemStream); <-- fails right here... return Address; }

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            Basically, what you're missing is seeking the stream pointer to the beginning after writing to the memstream but before calling Deserialize(). Try

            	public string Serialize()
            	{
            		MemoryStream MemStream = new MemoryStream();
            		BinaryFormatter Serializer = new BinaryFormatter();
            		Serializer.Serialize(MemStream, this);
            		return Convert.ToBase64String(MemStream.ToArray());
            	}
            
            
            
            	public static Address Deserialize(string SerializedData)
            	{
            		byte\[\] Buffer = Convert.FromBase64String(SerializedData);
            		MemoryStream MemStream = new MemoryStream(Buffer);
            		BinaryFormatter Serializer = new BinaryFormatter();
            		return (Address)Serializer.Deserialize(MemStream); 
            	}
            

            *edit* condensed Deserialize() a bit :)

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            modified on Wednesday, July 30, 2008 6:29 PM

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups