public static string CreateCSharpClass(SqlTableReference sqlTableReference, string?className = default(string))
    {
        var tableName     = className ?? sqlTableReference.TableName;
        var schemaColumns = InformationSchemaMetadataExplorer.GetInformationSchemaColumns(sqlTableReference);

        return(CreateCSharpClass(schemaColumns, tableName, CSharpClassTextGeneratorOptions.Default));
    }
示例#2
0
    private static void VerifyDataTableColumnsMatchDbTableSchema(ISqlConnectionProvider sqlConnectionProvider,
                                                                 string schemaName,
                                                                 string tableName,
                                                                 DataTable dataTable)
    {
        var dbTableColumns =
            InformationSchemaMetadataExplorer.GetInformationSchemaColumns(
                new SqlTableReference(sqlConnectionProvider, schemaName, tableName));
        var columnsFromDbDict = dbTableColumns.ToDictionary(z => z.COLUMN_NAME);

        var columnsInDataTableButMissingInDbTable = (
            from DataColumn dataTableColumn in dataTable.Columns
            let dbContainsDatatableColumn = columnsFromDbDict.ContainsKey(dataTableColumn.ColumnName)
                                            where !dbContainsDatatableColumn
                                            select dataTableColumn.ColumnName).ToList();



        if (columnsInDataTableButMissingInDbTable.Count > 0)
        {
            var message =
                $"SqlBulkInsert will fail because the following columns are present in the DataTable but not in Sql Database table {schemaName}.{tableName}: {string.Join(", ", columnsInDataTableButMissingInDbTable)}";

            throw new InvalidDataException(message);
        }
    }
    public static SqlTable Create(SqlTableReference sqlTableReference)
    {
        var columns    = InformationSchemaMetadataExplorer.GetInformationSchemaColumns(sqlTableReference);
        var sqlColumns = columns.Select(CSharpClassGeneratorFromInformationSchema.InformationSchemaColumnToSqlColumn).ToList();
        var sqlTable   = new SqlTable()
        {
            Name       = sqlTableReference.TableName,
            Schema     = sqlTableReference.SchemaName,
            SqlColumns = sqlColumns
        };

        return(sqlTable);
    }