public static DataTableResponseObject ToDataTableResponseObject(this DataTable table)
        {
            var dataTableResponseObject = new DataTableResponseObject
            {
                TableName = table.TableName
            };

            var columns = new List <DataTableResponseColumn>();

            foreach (DataColumn col in table.Columns)
            {
                columns.Add(new DataTableResponseColumn()
                {
                    ColumnName = col.ColumnName, DataType = col.DataType.ToString().Replace("System.", "")
                });
            }

            var rows = new dynamic[table.Rows.Count, table.Columns.Count];

            for (int i = 0; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    rows[i, j] = table.Rows[i][j] == DBNull.Value ? null : table.Rows[i][j];
                }
            }

            dataTableResponseObject.Columns = columns;
            dataTableResponseObject.Rows    = rows;

            return(dataTableResponseObject);
        }
Пример #2
0
        public static DataTableResponseObject ToDataTableResponseObject(this DataTable table)
        {
            var dataTableResponseObject = new DataTableResponseObject();

            dataTableResponseObject.TableName = table.TableName;

            var columns = new List <DataTableResponseColumn>();

            foreach (DataColumn col in table.Columns)
            {
                columns.Add(new DataTableResponseColumn()
                {
                    ColumnName = col.ColumnName, DataType = col.DataType.ToString().Replace("System.", "")
                });
            }

            var rows = new List <string[]>();

            foreach (DataRow row in table.Rows)
            {
                rows.Add(row.ItemArray.Select(x => x.ToString()).ToArray());
            }

            dataTableResponseObject.Columns = columns;
            dataTableResponseObject.Rows    = rows.ToArray();

            return(dataTableResponseObject);
        }
        public static DataTable ToDataTable(this DataTableResponseObject dataTableResponse)
        {
            if (dataTableResponse == null)
            {
                throw new ArgumentNullException("dataTableResponse");
            }

            var dataTable = new DataTable(dataTableResponse.TableName);

            dataTable.Columns.AddRange(dataTableResponse.Columns.Select(column => new DataColumn(column.ColumnName, GetColumnType(column.ColumnType, column.DataType))).ToArray());

            foreach (var row in dataTableResponse.Rows)
            {
                var rowWithCorrectTypes = new List <object>();
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    object rowValueWithCorrectType = null;

                    if (row[i] != null)
                    {
                        rowValueWithCorrectType = Convert.ChangeType(row[i], dataTable.Columns[i].DataType);
                    }

                    rowWithCorrectTypes.Add(rowValueWithCorrectType);
                }

                dataTable.Rows.Add(rowWithCorrectTypes.ToArray());
            }

            return(dataTable);
        }
Пример #4
0
        public static DataTable ToDataTable(this DataTableResponseObject dataTableResponse)
        {
            if (dataTableResponse == null)
            {
                throw new ArgumentNullException("dataTableResponse");
            }

            var dataTable = new DataTable(dataTableResponse.TableName);

            dataTable.Columns.AddRange(dataTableResponse.Columns.Select(column => new DataColumn(column.ColumnName, GetColumnType(column.DataType))).ToArray());

            foreach (var row in dataTableResponse.Rows)
            {
                var rowWithCorrectTypes = new List <object>();
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    object rowValueWithCorrectType = null;

                    if (row[i] != null)
                    {
                        rowValueWithCorrectType = Convert.ChangeType(row[i], dataTable.Columns[i].DataType, CultureInfo.InvariantCulture);

                        if (dataTable.Columns[i].DataType == typeof(DateTime))
                        {
                            var dateTimeString = row[i].ToString();

                            //check if the string is in UTC time
                            if (dateTimeString.EndsWith("Z", false, CultureInfo.InvariantCulture))
                            {
                                rowValueWithCorrectType = Convert.ToDateTime(row[i]).ToUniversalTime();
                            }
                            else
                            {
                                rowValueWithCorrectType = Convert.ToDateTime(row[i]);
                            }
                        }
                    }

                    rowWithCorrectTypes.Add(rowValueWithCorrectType);
                }

                dataTable.Rows.Add(rowWithCorrectTypes.ToArray());
            }

            return(dataTable);
        }
        public static DataTable ToDataTable(this DataTableResponseObject dataTableResponse)
        {
            if (dataTableResponse == null)
            {
                throw new ArgumentNullException("dataTableResponse");
            }

            var dataTable = new DataTable(dataTableResponse.TableName);

            dataTable.Columns.AddRange(dataTableResponse.Columns.Select(column => new DataColumn(column.ColumnName, GetColumnType(column.DataType))).ToArray());

            for (int i = 0; i < dataTableResponse.Rows.GetLength(0); i++)
            {
                var row = dataTable.NewRow();
                for (int j = 0; j < dataTable.Columns.Count; j++)
                {
                    row[j] = dataTableResponse.Rows[i, j] ?? DBNull.Value;
                }

                dataTable.Rows.Add(row);
            }

            return(dataTable);
        }
Пример #6
0
 public DiagnosticDataApiResponse()
 {
     Table = new DataTableResponseObject();
     RenderingProperties = new Rendering();
 }