code_junkie

Storing C# data structure into a SQL database

Nov 11th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.Serialization;
  6. using System.IO;
  7.  
  8. namespace SerializeThingy
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. List<string> myList = new List<string>();
  15. myList.Add("One");
  16. myList.Add("Two");
  17. myList.Add("Three");
  18. NetDataContractSerializer serializer = new NetDataContractSerializer();
  19. MemoryStream stream = new MemoryStream();
  20. serializer.Serialize(stream, myList);
  21. stream.Position = 0;
  22. Console.WriteLine(ASCIIEncoding.ASCII.GetString(stream.ToArray()));
  23. List<string> myList2 = (List<string>)serializer.Deserialize(stream);
  24. Console.WriteLine(myList2[0]);
  25. Console.ReadKey();
  26. }
  27. }
  28. }
  29.  
  30. [Serializable]
  31. public struct Vector3
  32. {
  33. public double x, y, z;
  34. }
  35.  
  36. class Program
  37. {
  38. static void Main(string[] args)
  39. {
  40. Vector3 vector = new Vector3();
  41. vector.x = 1;
  42. vector.y = 2;
  43. vector.z = 3;
  44.  
  45. MemoryStream memoryStream = new MemoryStream();
  46. BinaryFormatter binaryFormatter = new BinaryFormatter();
  47. binaryFormatter.Serialize(memoryStream, vector);
  48. string str = System.Convert.ToBase64String(memoryStream.ToArray());
  49.  
  50. //Store str into the database
  51. }
  52. }
  53.  
  54. using System.Runtime.Serialization.Formatters.Binary;
  55. using System.IO;
  56. using System.Data.SqlClient;
  57. using System.Runtime.Serialization;
  58.  
  59. public byte[] SerializeList<T>(List<T> list)
  60. {
  61.  
  62. MemoryStream ms = new MemoryStream();
  63.  
  64. BinaryFormatter bf = new BinaryFormatter();
  65.  
  66. bf.Serialize(ms, list);
  67.  
  68. ms.Position = 0;
  69.  
  70. byte[] serializedList = new byte[ms.Length];
  71.  
  72. ms.Read(serializedList, 0, (int)ms.Length);
  73.  
  74. ms.Close();
  75.  
  76. return serializedList;
  77.  
  78. }
  79.  
  80. public List<T> DeserializeList<T>(byte[] data)
  81. {
  82. try
  83. {
  84. MemoryStream ms = new MemoryStream();
  85.  
  86. ms.Write(data, 0, data.Length);
  87.  
  88. ms.Position = 0;
  89.  
  90. BinaryFormatter bf = new BinaryFormatter();
  91.  
  92. List<T> list = bf.Deserialize(ms) as List<T>;
  93.  
  94. return list;
  95. }
  96. catch (SerializationException ex)
  97. {
  98. // Handle deserialization problems here.
  99. Debug.WriteLine(ex.ToString());
  100.  
  101. return null;
  102. }
  103.  
  104. }
  105.  
  106. List<string> stringList = new List<string>() { "January", "February", "March" };
  107.  
  108. byte[] data = SerializeList<string>(stringList);
  109.  
  110. SqlParameter param = new SqlParameter("columnName", SqlDbType.Binary, data.Length);
  111. param.Value = data;
  112.  
  113. etc...
Add Comment
Please, Sign In to add comment