//private static HtmlTextBox CreateHtmlTextBoxFromDataTable(DataTable dataTable)
    //{
    //    var fileName = Path.GetTempFileName();
    //    var fileStream = new FileStream(fileName, FileMode.OpenOrCreate);
    //    using (var streamWriter = new StreamWriter(fileStream, Encoding.UTF8))
    //    {
    //        var columnIndexes = new int[dataTable.Columns.Count];
    //        for (var i = 0; i < columnIndexes.Length; i++)
    //            columnIndexes[i] = i;
    //        HtmlFormatter.Write(dataTable.DefaultView, columnIndexes, streamWriter);
    //    }

    //    var htmlTextBox = new HtmlTextBox();
    //    htmlTextBox.Navigate(fileName);

    //    return htmlTextBox;
    //}

    public static Control CreateControlFromDataTable(IQueryForm queryForm, DbCommandBuilder commandBuilder, DataTable dataTable,
                                                     GetTableSchemaResult getTableSchemaResult,
                                                     ResultWriterType tableStyle, bool readOnly, ColorTheme colorTheme)
    {
        Control control;

        switch (tableStyle)
        {
        case ResultWriterType.DataGrid:
            control = CreateDataTableEditorFromDataTable(queryForm, commandBuilder, dataTable, getTableSchemaResult, readOnly, colorTheme);
            break;

        //case ResultWriterType.Html:
        //    control = CreateHtmlTextBoxFromDataTable(dataTable);
        //    break;

        case ResultWriterType.ListView:
            control = CreateListViewFromDataTable(dataTable);
            break;

        default:
            throw new NotImplementedException();
        }

        return(control);
    }
    private static DataTableEditor CreateDataTableEditorFromDataTable(IQueryForm queryForm, DbCommandBuilder commandBuilder, DataTable dataTable,
                                                                      GetTableSchemaResult getTableSchemaResult, bool readOnly, ColorTheme colorTheme)
    {
        var editor = new DataTableEditor(queryForm, commandBuilder, colorTheme);

        editor.ReadOnly    = readOnly;
        editor.DataTable   = dataTable;
        editor.TableName   = dataTable.TableName;
        editor.TableSchema = getTableSchemaResult;
        return(editor);
    }
 private static DataTableEditor CreateDataTableEditorFromDataTable(
     DbCommandBuilder commandBuilder,
     DataTable dataTable,
     GetTableSchemaResult getTableSchemaResult,
     bool readOnly,
     ToolStripStatusLabel toolStripStatusLabel,
     ColorTheme colorTheme)
 {
     var editor = new DataTableEditor(commandBuilder, colorTheme);
     editor.ReadOnly = readOnly;
     editor.DataTable = dataTable;
     editor.TableName = dataTable.TableName;
     editor.TableSchema = getTableSchemaResult;
     editor.StatusBarPanel = toolStripStatusLabel;
     return editor;
 }
Example #4
0
        public static GetTableSchemaResult GetTableSchema(IDbConnection connection, string tableName)
        {
            var sqlCommandBuilder = new SqlCommandBuilder();

            var fourPartName = new DatabaseObjectMultipartName(connection.Database, tableName);
            var owner        = fourPartName.Schema;

            if (owner == null)
            {
                owner = "dbo";
            }

            var commandText = string.Format(@"declare @id int

select
    @id = o.object_id
from {0}.sys.objects o
join {0}.sys.schemas s
    on o.schema_id = s.schema_id
where
    s.name = {1}
    and o.name = {2}

select
    c.name as ColumnName,
    c.column_id as ColumnId,
    t.name as TypeName,
    c.is_nullable as IsNullable,
	c.is_computed as IsComputed,
    c.default_object_id as DefaultObjectId
from {0}.sys.columns c
join {0}.sys.types t
	on c.user_type_id = t.user_type_id
where c.object_id = @id
order by c.column_id

declare @index_id int
select top 1
    @index_id = i.index_id
from {0}.sys.indexes i (readpast)
cross apply
(
    select count(1) as [Count]
    from {0}.sys.index_columns ic (readpast)
    join {0}.sys.columns c (readpast)
        on ic.object_id = c.object_id
        and ic.column_id = c.column_id
    where
        ic.object_id = i.object_id
        and ic.index_id = i.index_id
        and c.is_identity = 1
) c
where
    i.object_id = @id
    and i.is_unique = 1
    and i.has_filter = 0
order by c.[Count],i.index_id

if @index_id is null
    select @index_id = i.index_id
    from  {0}.sys.indexes i (readpast)
    where
        i.object_id = @id
        and i.is_unique = 1

select ic.column_id as ColumnId
from {0}.sys.index_columns ic
where
    ic.object_id    = @id
    and ic.index_id = @index_id
order by ic.index_column_id",
                                            sqlCommandBuilder.QuoteIdentifier(fourPartName.Database),
                                            owner.ToNullableNVarChar(),
                                            fourPartName.Name.ToNullableNVarChar());

            var executor = DbCommandExecutorFactory.Create(connection);
            GetTableSchemaResult getTableSchemaResult = null;

            executor.ExecuteReader(new ExecuteReaderRequest(commandText), dataReader =>
            {
                var columns            = dataReader.ReadResult(128, ReadColumn).ToReadOnlyList();
                var uniqueIndexColumns = dataReader.ReadNextResult(128, ReadUniqueIndexColumn).ToReadOnlyList();
                getTableSchemaResult   = new GetTableSchemaResult(columns, uniqueIndexColumns);
            });
            return(getTableSchemaResult);
        }
        public static Control CreateControlFromDataTable(DbCommandBuilder commandBuilder, DataTable dataTable, GetTableSchemaResult getTableSchemaResult,
            ResultWriterType tableStyle, bool readOnly, ToolStripStatusLabel toolStripStatusLabel, ColorTheme colorTheme)
        {
            Control control;

            switch (tableStyle)
            {
                case ResultWriterType.DataGrid:
                    control = CreateDataTableEditorFromDataTable(commandBuilder, dataTable, getTableSchemaResult, readOnly, toolStripStatusLabel, colorTheme);
                    break;

                case ResultWriterType.Html:
                    control = CreateHtmlTextBoxFromDataTable(dataTable);
                    break;

                case ResultWriterType.ListView:
                    control = CreateListViewFromDataTable(dataTable);
                    break;

                default:
                    throw new NotImplementedException();
            }

            return control;
        }