Esempio n. 1
0
        public static string GenerateDataScript(this Table table, ISqlCeDatabase database)
        {
            var data = database.GetTableData(table) as DataTable;

            if (data == null || data.Rows.Count == 0)
            {
                return(string.Empty);
            }

            var builder = new StringBuilder();

            foreach (DataRow row in data.Rows)
            {
                builder.AppendLine();
                builder.AppendFormat("INSERT INTO {0}", table.Name);

                builder.Append(" (");
                foreach (var column in table.Columns.Where(column => column.Value.ManagedType != typeof(byte[])))
                {
                    builder.AppendFormat("{0},", column.Value.Name);
                }
                builder.Remove(builder.Length - 1, 1);

                builder.Append(") VALUES (");
                foreach (var column in table.Columns.Where(column => column.Value.ManagedType != typeof(byte[])))
                {
                    var value = row[column.Value.Ordinal - 1];
                    if (value == null)
                    {
                        builder.Append("NULL,");
                        continue;
                    }

                    if (column.Value.ManagedType == typeof(string) || column.Value.ManagedType == typeof(DateTime))
                    {
                        builder.AppendFormat("'{0}',", value.ToString().Replace("'", "''"));
                    }
                    else
                    {
                        builder.Append(value + ",");
                    }
                }
                builder.Remove(builder.Length - 1, 1);
                builder.Append(");");
            }

            builder.AppendLine();
            return(builder.ToString());
        }
Esempio n. 2
0
        public void LoadTableDataAndProperties(Table table, bool readOnly = false, bool resizeColumns = false, bool displaySchemaInfo = true)
        {
            if (database == null || table == null || lastSelectedTable == table)
            {
                return;
            }
            lastSelectedTable = table;

            ResetTableData();

            Task.Factory.StartNew(() =>
            {
                try
                {
                    TableDataIsBusy = true;
                    var sw          = Stopwatch.StartNew();
                    var dataTable   = database.GetTableData(table) as DataTable;
                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        TableDataGrid.DataSource = dataTable;
                        TableDataGrid.ReadOnly   = readOnly;
                        if (resizeColumns)
                        {
                            TableDataGrid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                        }
                    });
                    //CurrentMainTabIndex = 1;
                    TableDataCount         = dataTable != null ? dataTable.Rows.Count : 0;
                    TableDataExecutionTime = sw.Elapsed;

                    if (displaySchemaInfo)
                    {
                        var propertiesTable = database.GetTableProperties(table) as DataTable;
                        Application.Current.Dispatcher.Invoke((Action) delegate { TablePropertiesGrid.DataSource = propertiesTable; });
                    }
                }
                catch (Exception e)
                {
                    ResultSetErrors        = e.ToString();
                    CurrentResultsTabIndex = 3;
                }
                finally
                {
                    TableDataIsBusy = false;
                }
            });
        }