public static Database ImportViews(ILogger <SqlServerDatabaseFactory> logger, string connectionString, params string[] views) { var databaseFactory = new SqlServerDatabaseFactory { ConnectionString = connectionString, ImportSettings = new DatabaseImportSettings { ImportTables = false } }; var database = new Database(); using (var connection = databaseFactory.GetConnection()) { connection.Open(); database.Name = connection.Database; if (views.Length == 0) { database.DbObjects.AddRange(databaseFactory.GetDbObjects(connection).ToList()); } else { database.DbObjects.AddRange(databaseFactory.GetDbObjects(connection).Where(item => views.Contains(item.FullName)).ToList()); } database.Views.AddRange(databaseFactory.GetViews(connection, database.GetViews()).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); } }