예제 #1
0
        // Binary serialization to DB. Use VARBINARY(MAX)
        public void Ex2_SerializeDB()
        {
            Person2 p = new Person2(true)
            {
                Id   = 1,
                Name = "John Doe"
            };

            byte[]     data;
            IFormatter formatter = new BinaryFormatter();

            using (MemoryStream stream = new MemoryStream())
            {
                formatter.Serialize(stream, p);
                data = stream.ToArray();
            }

            // Write to db as binary.
            string cnxnString = ConfigurationManager.ConnectionStrings["DBPREPLocal"].ConnectionString;

            using (var conxn = new SqlConnection(cnxnString))
            {
                conxn.Open();

                string cmd = @"INSERT INTO [dbo].[Test01] ([SVAL], [OBJVAL])
                                 VALUES ('Bin Serialize Test 1' , @pOBJVAL)";

                SqlCommand sc = new SqlCommand(cmd, conxn);
                sc.Parameters.AddWithValue("@pOBJVAL", data);

                int r = sc.ExecuteNonQuery();
            }
        }
예제 #2
0
        // Binary deserialization from DB.
        public void Ex2_DeserializeDB()
        {
            byte[]     data      = null;
            IFormatter formatter = new BinaryFormatter();

            string cnxnString = ConfigurationManager.ConnectionStrings["DBPREPLocal"].ConnectionString;

            using (var conxn = new SqlConnection(cnxnString))
            {
                conxn.Open();

                string qry = @"SELECT	[OBJVAL]
                                FROM	[dbo].[Test01]
                                WHERE	[SVAL] = @pSVAL
                                ORDER BY ID
                                OFFSET 0 ROWS FETCH NEXT 1 ROW ONLY";

                SqlCommand sc = new SqlCommand(qry, conxn);
                sc.Parameters.AddWithValue("@pSVAL", "Bin Serialize Test 1");

                SqlDataReader r = sc.ExecuteReader();

                if (r.Read())
                {
                    data = (byte[])r["OBJVAL"];

                    using (MemoryStream stream = new MemoryStream(data))
                    {
                        Person2 dp = (Person2)formatter.Deserialize(stream);

                        Console.WriteLine("RT: {0} {1} {2}", dp.Id, dp.Name, dp.Dirty);
                    }
                }
            }
        }
예제 #3
0
        // Roundtrip binary serialization.
        public void Ex1_Serialize()
        {
            string path = @"..\..\..\TmpFile";
            string file = Path.Combine(path, @"Person2.dat");

            Person2 p = new Person2(true)
            {
                Id   = 1,
                Name = "John Doe"
            };

            IFormatter formatter = new BinaryFormatter();

            using (Stream stream = new FileStream(file, FileMode.Create))
            {
                formatter.Serialize(stream, p);
            }

            using (Stream stream = new FileStream(file, FileMode.Open))
            {
                Person2 dp = (Person2)formatter.Deserialize(stream);

                Console.WriteLine("{0} {1} {2}", dp.Id, dp.Name, dp.Dirty);
            }
        }