-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSetData.cs
More file actions
85 lines (77 loc) · 2.75 KB
/
DataSetData.cs
File metadata and controls
85 lines (77 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Runtime.Serialization;
using System.Xml;
using System.Collections.ObjectModel;
using System.Text;
using System.IO;
[DataContract]
public class DataSetData
{
[DataMember]
public ObservableCollection<DataTableInfo> Tables { get; set; }
[DataMember]
public string DataXML { get; set; }
public static DataSetData FromDataSet(DataSet ds)
{
DataSetData dsd = new DataSetData();
dsd.Tables = new ObservableCollection<DataTableInfo>();
foreach (DataTable t in ds.Tables)
{
DataTableInfo tableInfo = new DataTableInfo {TableName = t.TableName};
dsd.Tables.Add(tableInfo);
tableInfo.Columns = new ObservableCollection<DataColumnInfo>();
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;
}
}