tags:

views:

126

answers:

3

This is just for debugging purpose. We have an abused stored proc that takes for ever to return resultset. Optimizing the proc is out of question at this moment. Is it possible for me to store loaded dataset result some where so I dont have to keep calling the stored proc over and over again. And I could just continue testing my .net code. I dont want to cache it in memory but store it in a file locally.

DataSet ds = db.ExecuteDataSet(cmdobject);
//Store this result some where
//so next time I dont have to call the above method. Just pick up the stored result //from some where and continue with my testing.

Any code example would help.

+6  A: 

Well DataSet implements the ISerializable interface, so you could serialize it to a file.

Example:

  DataSet ds = new DataSet();
  if (File.Exists("SavedDataSet.dat"))
  {
     // If the serialized object exists, read it from the file
     // and deserialize it
     DataSet ds;
     Stream stream = File.Open("SavedDataSet.dat", FileMode.Open);
     BinaryFormatter bFormatter = new BinaryFormatter();
     ds = (DataSet)bFormatter.Deserialize(stream);
     stream.Close();
  }
  else
  {
     // If the serialized object doesn't exist, run the stored 
     // procedure and then serialize the result    
     ds = db.ExecuteDataSet(cmdobject);
     Stream stream = File.Open("SavedDataSet.dat", FileMode.Create);
     BinaryFormatter bFormatter = new BinaryFormatter();
     bFormatter.Serialize(stream, ds);
     stream.Close();
  }
Donut
this is faster than xml but harder to read/mess with. which is best depends on your needs but both are perfectly acceptable
ShuggyCoUk
+7  A: 

DataSet.WriteXml()?

MSDN link

grrrrrrrrrrrrr
don't forget `DataSet.ReadXml()` :)
STW
I implement this solution and it works except all my data types in data row are now string as opposed to original datatypes in datarow object.Before ... row["CustID"] was integerNow .. after reading from XML file row["CustID"] is string. They are all strings. Is there any way to fix this?
dotnet-practitioner
I haven't really used the writexml method, so no gaurantees here, but have you tried specifying the XMLWritemode to writeschema using DataSet.WriteXml (String, XmlWriteMode) and doing the same for the schema? See the link above for the method definitions.
grrrrrrrrrrrrr
* and doing the same for the ReadXML method.
grrrrrrrrrrrrr
+1  A: 

I can't see the details of your stored proc, but I suggest modifying it to create a new result table.

vis: (basturdized sql)

INSERT INTO CachedTable (SELECT /* whatever the stored proc did */);
dar7yl