Пример #1
0
        static void ReadItBack()
        {
            // get our data..
            SerializedTestDS stDS = new SerializedTestDS();
            SerializedTestDSTableAdapters.SerializedTestTableAdapter stTA = 
                new SerializedTestDSTableAdapters.SerializedTestTableAdapter();
            
            
            stTA.Fill(stDS.SerializedTest,1);

            SerializedTestDS.SerializedTestDataTable stTbl = 
                stDS.SerializedTest;
            byte [] final_buff = new byte[stTbl.Rows.Count * BIN_COLUMN_SIZE];
            int curr_offset = 0;
            foreach(SerializedTestDS.SerializedTestRow stRow in stTbl.Rows)
            {
                byte[] currBuff = stRow.ObjectData;
                Console.Write("UserID[" + stRow.UserID + "] ");
                Console.Write("sequence[" + stRow.sequence + "] ");
                Console.WriteLine("buffsize[" + currBuff.Length + "]");
                
                DumpBuffer(currBuff);
                
                for (int currByte = 0;
                     currByte < currBuff.Length;
                     currByte++)
                {
                    final_buff[curr_offset] = currBuff[currByte];
                    curr_offset++;
                }
                Console.WriteLine();
                Console.WriteLine();
            }
            
            // deserialize and print..
            MemoryStream ms = new MemoryStream(final_buff);
            IFormatter formatter = new BinaryFormatter();
            MyContainer my_c = (MyContainer) formatter.Deserialize(ms);
            Console.WriteLine(my_c.ToString());
        
        }
Пример #2
0
        static void WriteItOut()
        {
            // create or contained classes..
            // 
            MyContainer mc = new MyContainer();
            Console.WriteLine(mc.ToString());  
            // serialize our classes to a byte array
            MemoryStream serializeStream = new MemoryStream();
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(serializeStream,mc);

            byte [] strm_buff = serializeStream.GetBuffer();
            Console.WriteLine("stream length[" + serializeStream.Length + "]");
            Console.WriteLine("buffer length[" + strm_buff.Length + "]");
            int buff_len = (int) Math.Min(serializeStream.Length,
                                          strm_buff.Length);
            DumpBuffer(strm_buff);
            
            
            // pop it to DB..
            SerializedTestDS stDS = new SerializedTestDS();
            SerializedTestDSTableAdapters.SerializedTestTableAdapter stTA = 
                new SerializedTestDSTableAdapters.SerializedTestTableAdapter();
            
            // for this test, we assume that we are editing user #1..
            // get all userID 1 rows into DS..
            stTA.Fill(stDS.SerializedTest,1);

            SerializedTestDS.SerializedTestDataTable stTbl = 
                stDS.SerializedTest;
            
            // since we are writing, delete the old rows since we are now replacing the old w/ the new.
            foreach(SerializedTestDS.SerializedTestRow row in stTbl.Rows)
            {
                row.Delete();
            }
            // write out our buffer to the DB..
            int bytes_to_write = buff_len;
            int curr_offset = 0;
            int curr_sequence = 1;
            byte [] write_buff = null;
            while (bytes_to_write > 0)
            {
                // we need a new buffer for each row, since the
                // dataset doesn't do a copy of the buffer, but 
                // obviously just keeps a reference to the buffer 
                // before writing.
                write_buff = new byte [BIN_COLUMN_SIZE];
                for(int currByte = 0;
                    currByte < write_buff.Length;
                    currByte++)
                {
                    write_buff[currByte] = strm_buff[currByte + curr_offset];
                }
                // write it..
                SerializedTestDS.SerializedTestRow stRow = 
                    stTbl.NewSerializedTestRow();
                stRow.UserID = 1;
                stRow.ObjectData = write_buff;
                stRow.sequence = curr_sequence;
                stTbl.AddSerializedTestRow(stRow);
                Console.WriteLine("Writing @ current_offset[" 
                                  + curr_offset 
                                  + "] "
                                  + write_buff.Length
                                  + " bytes "
                                  + "into row sequence["
                                  + curr_sequence
                                  + "] ");
                // adjust offsets.
                curr_offset += write_buff.Length;
                bytes_to_write -= write_buff.Length;
                curr_sequence++;
            }
            stTA.Update(stDS);
            Console.WriteLine();
            Console.WriteLine();
        }