H
hs
Hi
I am serializing a dataset using a binary formatter as follows:
IFormatter formater = new BinaryFormatter();
formatter.Serialize(stream, ds); // ds=DataSet, stream=MemoryStream
....
DataSet ds2 = (DataSet)formatter2.Deserialize(stream2);
For the size of my DataSet, its taking 0.8 seconds to serialize and 2.3
seconds to deserialize.
After reading some previous archive messages, I decided to try some manual
binary serialization using a custom DataSet (myDataSet). I have slightly
improved on my deserialization time (now takes 0.9 seconds) but my
serialzation has gone mad (takes 10 seconds). All this time is being spent
after the myDataSet.GetObjectData method. What is hapenning after this
method?
any advice on how to improve this time would be appreciated.
thanks
Code
------
IFormatter formater = new BinaryFormatter();
formatter.Serialize(stream, myds); // myds=myDataSet, stream=MemoryStream
....
myDataSet myds2 = (myDataSet)formatter2.Deserialize(stream2);
myDataSet class
----------------------
[Serializable]
public class myDataSet : DataSet, ISerializable
{
public myDataSet() : base()
{}
public myDataSet(string name) : base(name)
{}
protected myDataSet(SerializationInfo sinfo, StreamingContext context)
{
// Extract from the serialization info
ArrayList tables = new ArrayList();
tables = (ArrayList) sinfo.GetValue("Tables", typeof(ArrayList));
// foreach table name in tables create a DataTable and add to table
collection
for(int i=0;i<tables.Count; i++)
{
string tableName = (string)tables;
DataTable dt = new DataTable(tableName);
ArrayList colNames = new ArrayList();
ArrayList colTypes = new ArrayList();
ArrayList dataRows = new ArrayList();
colNames = (ArrayList) sinfo.GetValue("ColNames_"+tableName,
typeof(ArrayList));
colTypes = (ArrayList) sinfo.GetValue("ColTypes_"+tableName,
typeof(ArrayList));
dataRows = (ArrayList) sinfo.GetValue("DataRows_"+tableName,
typeof(ArrayList));
// Add columns
for(int iCol=0; iCol<colNames.Count; iCol++)
{
DataColumn col = new DataColumn(colNames[iCol].ToString(),
Type.GetType(colTypes[iCol].ToString() ));
dt.Columns.Add(col);
}
// Add rows
for(int iRows=0; iRows<dataRows.Count; iRows++)
{
DataRow row = dt.NewRow();
row.ItemArray = (object[]) dataRows[iRows];
dt.Rows.Add(row);
}
this.AcceptChanges();
// Add table to Tables collection
this.Tables.Add(dt);
}
void ISerializable.GetObjectData(SerializationInfo sinfo,
StreamingContext context)
{
// Add arrays
ArrayList tables = new ArrayList();
foreach(DataTable dt in this.Tables)
{
ArrayList colNames = new ArrayList();
ArrayList colTypes = new ArrayList();
ArrayList dataRows = new ArrayList();
string tableName = dt.TableName ;
// Insert the table name into worker array
tables.Add(tableName);
// Insert column information (names and types) into worker arrays
for(int iCols=0; iCols<dt.Columns.Count; iCols++)
{
DataColumn Col = dt.Columns[iCols];
colNames.Add(Col.ColumnName);
colTypes.Add(Col.DataType.FullName);
}
for(int iRows=0; iRows<dt.Rows.Count; iRows++)
{
dataRows.Add(dt.Rows[iRows].ItemArray);
}
// Add arrays to the serialization info structure
sinfo.AddValue("ColNames_"+tableName, colNames);
sinfo.AddValue("ColTypes_"+tableName, colTypes);
sinfo.AddValue("DataRows_"+tableName, dataRows);
}
sinfo.AddValue("Tables", tables);
}
}
I am serializing a dataset using a binary formatter as follows:
IFormatter formater = new BinaryFormatter();
formatter.Serialize(stream, ds); // ds=DataSet, stream=MemoryStream
....
DataSet ds2 = (DataSet)formatter2.Deserialize(stream2);
For the size of my DataSet, its taking 0.8 seconds to serialize and 2.3
seconds to deserialize.
After reading some previous archive messages, I decided to try some manual
binary serialization using a custom DataSet (myDataSet). I have slightly
improved on my deserialization time (now takes 0.9 seconds) but my
serialzation has gone mad (takes 10 seconds). All this time is being spent
after the myDataSet.GetObjectData method. What is hapenning after this
method?
any advice on how to improve this time would be appreciated.
thanks
Code
------
IFormatter formater = new BinaryFormatter();
formatter.Serialize(stream, myds); // myds=myDataSet, stream=MemoryStream
....
myDataSet myds2 = (myDataSet)formatter2.Deserialize(stream2);
myDataSet class
----------------------
[Serializable]
public class myDataSet : DataSet, ISerializable
{
public myDataSet() : base()
{}
public myDataSet(string name) : base(name)
{}
protected myDataSet(SerializationInfo sinfo, StreamingContext context)
{
// Extract from the serialization info
ArrayList tables = new ArrayList();
tables = (ArrayList) sinfo.GetValue("Tables", typeof(ArrayList));
// foreach table name in tables create a DataTable and add to table
collection
for(int i=0;i<tables.Count; i++)
{
string tableName = (string)tables;
DataTable dt = new DataTable(tableName);
ArrayList colNames = new ArrayList();
ArrayList colTypes = new ArrayList();
ArrayList dataRows = new ArrayList();
colNames = (ArrayList) sinfo.GetValue("ColNames_"+tableName,
typeof(ArrayList));
colTypes = (ArrayList) sinfo.GetValue("ColTypes_"+tableName,
typeof(ArrayList));
dataRows = (ArrayList) sinfo.GetValue("DataRows_"+tableName,
typeof(ArrayList));
// Add columns
for(int iCol=0; iCol<colNames.Count; iCol++)
{
DataColumn col = new DataColumn(colNames[iCol].ToString(),
Type.GetType(colTypes[iCol].ToString() ));
dt.Columns.Add(col);
}
// Add rows
for(int iRows=0; iRows<dataRows.Count; iRows++)
{
DataRow row = dt.NewRow();
row.ItemArray = (object[]) dataRows[iRows];
dt.Rows.Add(row);
}
this.AcceptChanges();
// Add table to Tables collection
this.Tables.Add(dt);
}
void ISerializable.GetObjectData(SerializationInfo sinfo,
StreamingContext context)
{
// Add arrays
ArrayList tables = new ArrayList();
foreach(DataTable dt in this.Tables)
{
ArrayList colNames = new ArrayList();
ArrayList colTypes = new ArrayList();
ArrayList dataRows = new ArrayList();
string tableName = dt.TableName ;
// Insert the table name into worker array
tables.Add(tableName);
// Insert column information (names and types) into worker arrays
for(int iCols=0; iCols<dt.Columns.Count; iCols++)
{
DataColumn Col = dt.Columns[iCols];
colNames.Add(Col.ColumnName);
colTypes.Add(Col.DataType.FullName);
}
for(int iRows=0; iRows<dt.Rows.Count; iRows++)
{
dataRows.Add(dt.Rows[iRows].ItemArray);
}
// Add arrays to the serialization info structure
sinfo.AddValue("ColNames_"+tableName, colNames);
sinfo.AddValue("ColTypes_"+tableName, colTypes);
sinfo.AddValue("DataRows_"+tableName, dataRows);
}
sinfo.AddValue("Tables", tables);
}
}