Search code examples
c#winformsvisual-studio-2010crashrestore

Best way to save and restore a DataTable c#


I have developed a WinForm c# application , now adding a recovery options so if it closes unexpectedly etc Everything can be recovered on a new run.

I have managed to recover almost everything (list,Int,Strings etc...) only issue i am facing is restoring a DataTable. During the run on my application records are added to this DataTable and at the end user can export this to csv.

I tried to add the DataTable to Properties.Settings.Default... But it does not work on the new run i always see it as Null .

Any suggestion on best way to save and restore a DataTable keeping in mind they records can go over 10-15 k during a run .

Thank you


Solution

  • Properties.Settings can store string data, so you can serialize your DataTable and store it. Later you can deserialize the string to get DataTable. You can use JSON.Net like:

    var serializedDt = JsonConvert.SerializeObject(dt);
    //store the string
    

    to retrieve back:

    DataTable yourDataTable = JsonConvert.DeserializeObject<DataTable>(serializedDt);
    

    One more thing to add, if you are expecting large data, then you may look at options to store data in a database at client side, like Sqlite.