using System; using System.Collections.Generic; using System.Data; namespace Clifton.Tools.Data { public static class RawDataTable { public static void Serialize(RawSerializer rs, DataTable dt) { int goodRowCount = dt.Rows.Count; foreach (DataRow testrow in dt.Rows) { if ((testrow.RowState == DataRowState.Deleted) || (testrow.RowState == DataRowState.Detached)) { --goodRowCount; } } rs.Serialize(dt.TableName); rs.Serialize(dt.Columns.Count); rs.Serialize(goodRowCount); rs.Serialize(dt.PrimaryKey.Length); foreach (DataColumn dc in dt.PrimaryKey) { rs.Serialize(dc.ColumnName); } foreach (DataColumn dc in dt.Columns) { rs.Serialize(dc.ColumnName); rs.Serialize(dc.AllowDBNull); rs.Serialize(dc.ReadOnly); rs.Serialize(dc.Expression); rs.Serialize(dc.DataType.AssemblyQualifiedName); rs.SerializeNullable(dc.DefaultValue); } foreach (DataRow dr in dt.Rows) { if ((dr.RowState != DataRowState.Deleted) && (dr.RowState != DataRowState.Detached)) { foreach (DataColumn dc in dt.Columns) { if (dc.AllowDBNull) { rs.SerializeNullable(dr[dc]); } else { rs.Serialize(dr[dc]); } } } } } public static DataTable Deserialize(RawDeserializer rd) { string tableName = rd.DeserializeString(); int columns = rd.DeserializeInt(); int rows = rd.DeserializeInt(); int pkCount = rd.DeserializeInt(); string[] pkColNames=null; // Read the PK column names. if (pkCount > 0) { pkColNames = new string[pkCount]; for (int i = 0; i < pkCount; i++) { pkColNames[i] = rd.DeserializeString(); } } DataTable dtIn = new DataTable(tableName); // Initialize the columns. for (int x = 0; x < columns; x++) { string columnName = rd.DeserializeString(); bool allowNulls = rd.DeserializeBool(); bool readOnly = rd.DeserializeBool(); string expression = rd.DeserializeString(); string type = rd.DeserializeString(); Type colType=Type.GetType(type); object defaultValue = rd.DeserializeNullable(colType); DataColumn dc = new DataColumn(columnName, colType); dc.DefaultValue = defaultValue; dc.AllowDBNull = allowNulls; dc.ReadOnly = readOnly; dc.Expression = expression; dtIn.Columns.Add(dc); } if (pkCount > 0) { List<DataColumn> pkCols = new List<DataColumn>(); // DataColumn[] pkCols = new DataColumn[pkCount]; for (int i = 0; i < pkCount; i++) { // Allowable PK columns are only non-nullable pk fields. // Otherwise, we have synchronization problems between the server and the client, // and the client attempts to populate PK fields. // A PK field will be nullable if the table in the view is designated as ReadOnly. if (!dtIn.Columns[pkColNames[i]].AllowDBNull) { pkCols.Add(dtIn.Columns[pkColNames[i]]); // pkCols[i] = dtIn.Columns[pkColNames[i]]; } } dtIn.PrimaryKey = pkCols.ToArray(); } // Initialize the rows. for (int y = 0; y < rows; y++) { DataRow dr = dtIn.NewRow(); for (int x = 0; x < columns; x++) { DataColumn dc = dtIn.Columns[x]; object obj; if (dc.AllowDBNull) { obj = rd.DeserializeNullable(dc.DataType); } else { obj = rd.Deserialize(dc.DataType); } dr[dc] = obj; } dtIn.Rows.Add(dr); } return dtIn; } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)