It is quite obvious that we can't make use of DataSet in Silverlight but we can develop something similar to DataSet to use in Silverlight here it is.
[DataContract]
public class DataColumnInfo
{
[DataMember]
public string ColumnName { get; set; }
[DataMember]
public string ColumnTitle { get; set; }
[DataMember]
public string DataTypeName { get; set; }
[DataMember]
public bool IsRequired { get; set; }
[DataMember]
public bool IsKey { get; set; }
[DataMember]
public bool IsReadOnly { get; set; }
[DataMember]
public int DisplayIndex { get; set; }
[DataMember]
public string EditControlType { get; set; }
[DataMember]
public int MaxLength { get; set; }
[DataMember]
public bool IsChecked { get; set; }
}
[DataContract]
public class DataTableInfo
{
[DataMember]
public string TableName { get; set; }
[DataMember]
public ObservableCollection Columns { get; set; }
}
[DataMember]
public ObservableCollection Tables { get; set; }
[DataMember]
public string DataXML { get; set; }
public static DataSetData FromDataSet(DataSet ds)
{
DataSetData dsd = new DataSetData();
dsd.Tables = new ObservableCollection();
foreach (DataTable t in ds.Tables)
{
DataTableInfo tableInfo = new DataTableInfo {TableName = t.TableName};
dsd.Tables.Add(tableInfo);
tableInfo.Columns = new ObservableCollection();
foreach (DataColumn c in t.Columns)
{
DataColumnInfo col = new DataColumnInfo { ColumnName = c.ColumnName, ColumnTitle = c.ColumnName, DataTypeName = c.DataType.FullName, MaxLength=c.MaxLength, IsKey=c.Unique, IsReadOnly=(c.Unique || c.ReadOnly), IsRequired = !c.AllowDBNull};
if (c.DataType == typeof(System.Guid))
{
col.IsReadOnly = true;
col.DisplayIndex = -1;
}
tableInfo.Columns.Add(col);
}
}
dsd.DataXML = ds.GetXml();
return dsd;
}
public static DataSet ToDataSet(DataSetData dsd)
{
DataSet ds = new DataSet();
UTF8Encoding encoding = new UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(dsd.DataXML);
MemoryStream stream = new MemoryStream(byteArray);
XmlReader reader = new XmlTextReader(stream);
ds.ReadXml(reader);
XDocument xd = XDocument.Parse(dsd.DataXML);
foreach (DataTable dt in ds.Tables)
{
var rs = from row in xd.Descendants(dt.TableName)
select row;
int i = 0;
foreach (var r in rs)
{
DataRowState state = (DataRowState)Enum.Parse(typeof(DataRowState), r.Attribute("RowState").Value);
DataRow dr = dt.Rows[i];
dr.AcceptChanges();
if (state == DataRowState.Deleted)
dr.Delete();
else if (state == DataRowState.Added)
dr.SetAdded();
else if (state == DataRowState.Modified)
dr.SetModified();
i++;
}
}
return ds;
}
You can download the source code
source code
Your suggestion/comments are appreciated.
Cheers!
Vinod
[DataContract]
public class DataColumnInfo
{
[DataMember]
public string ColumnName { get; set; }
[DataMember]
public string ColumnTitle { get; set; }
[DataMember]
public string DataTypeName { get; set; }
[DataMember]
public bool IsRequired { get; set; }
[DataMember]
public bool IsKey { get; set; }
[DataMember]
public bool IsReadOnly { get; set; }
[DataMember]
public int DisplayIndex { get; set; }
[DataMember]
public string EditControlType { get; set; }
[DataMember]
public int MaxLength { get; set; }
[DataMember]
public bool IsChecked { get; set; }
}
[DataContract]
public class DataTableInfo
{
[DataMember]
public string TableName { get; set; }
[DataMember]
public ObservableCollection
}
[DataMember]
public ObservableCollection
[DataMember]
public string DataXML { get; set; }
public static DataSetData FromDataSet(DataSet ds)
{
DataSetData dsd = new DataSetData();
dsd.Tables = new ObservableCollection
foreach (DataTable t in ds.Tables)
{
DataTableInfo tableInfo = new DataTableInfo {TableName = t.TableName};
dsd.Tables.Add(tableInfo);
tableInfo.Columns = new ObservableCollection
foreach (DataColumn c in t.Columns)
{
DataColumnInfo col = new DataColumnInfo { ColumnName = c.ColumnName, ColumnTitle = c.ColumnName, DataTypeName = c.DataType.FullName, MaxLength=c.MaxLength, IsKey=c.Unique, IsReadOnly=(c.Unique || c.ReadOnly), IsRequired = !c.AllowDBNull};
if (c.DataType == typeof(System.Guid))
{
col.IsReadOnly = true;
col.DisplayIndex = -1;
}
tableInfo.Columns.Add(col);
}
}
dsd.DataXML = ds.GetXml();
return dsd;
}
public static DataSet ToDataSet(DataSetData dsd)
{
DataSet ds = new DataSet();
UTF8Encoding encoding = new UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(dsd.DataXML);
MemoryStream stream = new MemoryStream(byteArray);
XmlReader reader = new XmlTextReader(stream);
ds.ReadXml(reader);
XDocument xd = XDocument.Parse(dsd.DataXML);
foreach (DataTable dt in ds.Tables)
{
var rs = from row in xd.Descendants(dt.TableName)
select row;
int i = 0;
foreach (var r in rs)
{
DataRowState state = (DataRowState)Enum.Parse(typeof(DataRowState), r.Attribute("RowState").Value);
DataRow dr = dt.Rows[i];
dr.AcceptChanges();
if (state == DataRowState.Deleted)
dr.Delete();
else if (state == DataRowState.Added)
dr.SetAdded();
else if (state == DataRowState.Modified)
dr.SetModified();
i++;
}
}
return ds;
}
You can download the source code
source code
Your suggestion/comments are appreciated.
Cheers!
Vinod
1 comment:
Nice and easy way to implement.
Post a Comment
Send us your comment related to the topic mentioned on the blog