public static void SerializeNow(Invoice myInvoice) { int invID = myInvoice.InvoiceID; //setup a memory stream object that is needed //to hold the obj in memory during the //format and transformation process MemoryStream myStream = new MemoryStream(); //setup the binary formatting obj that //performs the format process BinaryFormatter myFormat = new BinaryFormatter(); //we call the serialize method, passing the //stream so we know where to save and obj it //will transform myFormat.Serialize(myStream, myInvoice); //now that the obj is serialized, covert to //Base64 for encoding and easy storage string serializedValue = Convert.ToBase64String(myStream.ToArray()); //call the Insert method of the DA class DataAdapter.Insert(invID, serializedValue); //close the stream myStream.Close(); }
/// <summary> /// create a test person object. save to the database as a serialized element. Retrieve the serialized data and de-serialize for display. /// </summary> /// <param name="args"></param> static void Main(string[] args) { Person myPerson = new Person(); myPerson.Age = 43; myPerson.Name = "Mike Johnson"; myPerson.Employer = "OTC"; //call the serializeNow method //to package this Person obj up and //store it in the DB Serializer.SerializeNow(myPerson); //call the DB Get method to get all the records DataAdapter.Get(); Console.ReadKey(); }