Пример #1
0
        public void T1_check_if_database_is_created()
        {
            ScriptExecutor exe = new ScriptExecutor();

            using (SqlConnection connection = new SqlConnection(DbInit.GetConnectionString()))
            {
                connection.Open();
                bool databaseExists;

                string     checkIfExistCommand = string.Format("SELECT database_id FROM sys.databases WHERE Name = '{0}'", _databaseName);
                SqlCommand sqlCmd    = new SqlCommand(checkIfExistCommand, connection);
                object     resultObj = sqlCmd.ExecuteScalar();

                int databaseID = 0;

                if (resultObj != null)
                {
                    int.TryParse(resultObj.ToString(), out databaseID);
                }

                databaseExists = (databaseID > 0);
                if (databaseExists == false)
                {
                    exe.ScriptExe(DbInit.CreateDatabse(), connection);
                    resultObj = sqlCmd.ExecuteScalar();
                    if (resultObj != null)
                    {
                        int.TryParse(resultObj.ToString(), out databaseID);
                    }
                    databaseID.Should().BeGreaterThan(0);
                }
                else
                {
                    String     dropBaseCommand = "ALTER DATABASE " + _databaseName + " SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE " + _databaseName;
                    SqlCommand sqlCommand      = new SqlCommand(dropBaseCommand, connection);
                    sqlCommand.ExecuteScalar();
                    resultObj = sqlCmd.ExecuteScalar();
                    exe.ScriptExe(DbInit.CreateDatabse(), connection);
                    if (resultObj != null)
                    {
                        int.TryParse(resultObj.ToString(), out databaseID);
                    }
                    databaseID.Should().BeGreaterThan(0);
                }
            }
        }