示例#1
0
        /// <summary>
        /// Represents a test Database that was created for tests.  The DB has already been attached/created,
        /// and will not be removed unless dropDatabaseOnCleanup is true. 
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="dropDatabaseOnCleanup">If true the db instance will be dropped when the Cleanup method is called</param>
        /// <param name="dbName"></param>
        public SqlTestDB(InstanceInfo instance, string dbName, bool dropDatabaseOnCleanup = false)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (string.IsNullOrEmpty(dbName))
            {
                throw new ArgumentOutOfRangeException("dbName");
            }

            _instance = instance;
            _dbName = dbName;

            _cleanupDatabase = true;
        }
示例#2
0
        public static bool TryCreateFromDacpac(InstanceInfo instance, string dacpacPath, out SqlTestDB db, out string error, DacDeployOptions deployOptions = null, bool dropDatabaseOnCleanup = false)
        {
            error = null;
            string dbName = string.Empty;
            try
            {
                dbName = Path.GetFileNameWithoutExtension(dacpacPath);
                db = SqlTestDB.CreateFromDacpac(instance, dacpacPath, deployOptions, dropDatabaseOnCleanup);
                return true;
            }
            catch (Exception ex)
            {
                error = ExceptionText.GetText(ex);
                db = null;

                bool dbCreated = SafeDatabaseExists(instance, dbName);
                if (dbCreated)
                {
                    db = new SqlTestDB(instance, dbName, dropDatabaseOnCleanup);
                }

                return false;
            }
        }
示例#3
0
 private static bool SafeDatabaseExists(InstanceInfo instance, string dbName)
 {
     try
     {
         SqlTestDB masterDb = new SqlTestDB(instance, "master");
         using (SqlConnection connection = masterDb.OpenSqlConnection())
         {
             using (SqlCommand command = connection.CreateCommand())
             {
                 command.CommandText = string.Format(CultureInfo.CurrentCulture, "select count(*) from sys.databases where [name]='{0}'", dbName);
                 object result = command.ExecuteScalar();
                 int count;
                 return result != null && int.TryParse(result.ToString(), out count) && count > 0;
             }
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
         return false;
     }
 }
示例#4
0
 public static SqlTestDB CreateFromDacpac(InstanceInfo instance, string dacpacPath, DacDeployOptions deployOptions = null, bool dropDatabaseOnCleanup = false)
 {
     string dbName = Path.GetFileNameWithoutExtension(dacpacPath);
     DacServices ds = new DacServices(instance.BuildConnectionString(dbName));
     using (DacPackage dp = DacPackage.Load(dacpacPath, DacSchemaModelStorageType.Memory))
     {
         ds.Deploy(dp, dbName, true, deployOptions);
     }
     var sqlDb = new SqlTestDB(instance, dbName, dropDatabaseOnCleanup);
     return sqlDb;
 }
示例#5
0
 public static SqlTestDB CreateFromBacpac(InstanceInfo instance, string bacpacPath, DacImportOptions importOptions = null, bool dropDatabaseOnCleanup = false)
 {
     string dbName = Path.GetFileNameWithoutExtension(bacpacPath);
     DacServices ds = new DacServices(instance.BuildConnectionString(dbName));
     using (BacPackage bp = BacPackage.Load(bacpacPath, DacSchemaModelStorageType.Memory))
     {
         importOptions = FillDefaultImportOptionsForTest(importOptions);
         ds.ImportBacpac(bp, dbName, importOptions);
     }
     var sqlDb = new SqlTestDB(instance, dbName, dropDatabaseOnCleanup);
     return sqlDb;
 }
示例#6
0
 public static void ExecuteNonQuery(InstanceInfo instance, string dbName, params string[] scripts)
 {
     ExecuteNonQuery(instance, dbName, (IList <string>)scripts);
 }
示例#7
0
 public static void ExecuteNonQuery(InstanceInfo instance, string dbName, int commandTimeout, params string[] scripts)
 {
     ExecuteNonQuery(instance, dbName, (IList <string>)scripts, commandTimeout);
 }
示例#8
0
 public static void DropDatabase(InstanceInfo instance, string databaseName, bool displayException = true)
 {
     DropDatabase(instance.BuildConnectionString(CommonConstants.MasterDatabaseName), databaseName, displayException);
 }