public static Database ImportTables(ILogger <SqlServerDatabaseFactory> logger, string connectionString, params string[] tables) { var databaseFactory = new SqlServerDatabaseFactory { ConnectionString = connectionString, ImportSettings = new DatabaseImportSettings { ImportViews = false } }; var database = new Database(); using (var connection = databaseFactory.GetConnection()) { connection.Open(); database.Name = connection.Database; if (tables.Length == 0) { database.DbObjects.AddRange(databaseFactory.GetDbObjects(connection).ToList()); } else { database.DbObjects.AddRange(databaseFactory.GetDbObjects(connection).Where(item => tables.Contains(item.FullName)).ToList()); } database.Tables.AddRange(databaseFactory.GetTables(connection, database.GetTables()).ToList()); } return(database); }
/// <summary> /// Imports an existing database from SQL Server instance /// </summary> /// <param name="logger">Instance for <see cref="Logger"/> class</param> /// <param name="connectionString">Connection string</param> /// <param name="names">Table or view names to include in import action</param> /// <returns>An instance of <see cref="Database"/> class that represents an existing database in SQL Server instance</returns> public static Database ImportTablesAndViews(ILogger <SqlServerDatabaseFactory> logger, string connectionString, params string[] names) { var databaseFactory = new SqlServerDatabaseFactory(logger) { DatabaseImportSettings = new DatabaseImportSettings { ConnectionString = connectionString } }; using (var connection = databaseFactory.GetConnection()) { connection.Open(); var database = new Database { DataSource = connection.DataSource, Catalog = connection.Database, Name = connection.Database }; if (names.Length == 0) { database.DbObjects.AddRange(databaseFactory.GetDbObjects(connection).ToList()); } else { database.DbObjects.AddRange(databaseFactory.GetDbObjects(connection).Where(item => names.Contains(item.FullName)).ToList()); } database.Tables.AddRange(databaseFactory.GetTables(connection, database.GetTables()).ToList()); database.Views.AddRange(databaseFactory.GetViews(connection, database.GetViews()).ToList()); return(database); } }