0

I do a series of complex calculations and place the results in a table. I then need to either display the results that been stored in the table either: a) immediately using "asp:Table" or b) saved to a SQL database for redisplay without the need for recalculations.

Is there a simple way to save the table to an SQL database (ideally as a single field) for retrieval in the future and subsequent display via "asp:Table"?

Peter125
  • 13
  • 1

1 Answers1

1

If you need to store an entire object for later use, that's Serialization.

Using something like Fastest way to serialize and deserialize .NET objects you create three components:

  1. Code to convert the Table into a string (or binary)
  2. Code to exactly reverse the conversion from step 1. so you can reuse the object
  3. Some place to store the string (or binary) data.

The code is simply

public static string Serialize(Table table)
{
    var serializer = new XmlSerializer(typeof(Table));
    TextWriter writer = new StringWriter();
    serializer.Serialize(writer, table);
    return writer.ToString();
}

public static Table Deserialize(string tableData)
{
    var serializer = new XmlSerializer(typeof(Table));
    TextReader reader = new StringReader(tableData);
    return (Table)serializer.Deserialize(reader);
} 

You can put this data into a SQL Server varchar(max) column, obviously with some primary key so you can later retrieve it.

Community
  • 1
  • 1
Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169