public void TestFixtureSetUp()
        {
            var localDb = new SqlLocalDbApiWrapper();

            if (!localDb.IsLocalDBInstalled())
            {
                throw new Exception("LocalDB is not installed!");
            }

            this.instance = TemporarySqlLocalDbInstance.Create(deleteFiles: true);

            // Initialize database
            var strategy = new DropCreateDatabaseAlways <SentinelContext>();

            Database.SetInitializer(strategy);

            var builder = this.instance.CreateConnectionStringBuilder();

            // Update the connection string to specify the name of the database
            // and its physical location to the current application directory
            builder.SetInitialCatalogName("SentinelAuth");
            builder.SetPhysicalFileName(@".\SentinelAuth.mdf");

            this.connectionString = builder.ConnectionString;

            using (var context = new SentinelContext(this.connectionString))
            {
                context.Database.Initialize(true);
            }
        }
        private ISqlLocalDbInstance GetAppropriateLocalDbInstance(
            SqlLocalDbApiWrapper localDbApi,
            string localDbInstanceName,
            IEnumerable <string> allowedLocalDbVersions)
        {
            SqlLocalDbProvider localDbProvider = new SqlLocalDbProvider();

            SqlLocalDbInstance existingInstance = GetExistingInstance(localDbProvider, localDbInstanceName);

            if (existingInstance != null)
            {
                return(existingInstance);
            }

            // No versions configured so just create an instance without specifying a version.
            // This will create an instance using the latest version.
            if (!allowedLocalDbVersions.Any())
            {
                SqlLocalDbInstance newInstance = localDbProvider.CreateInstance(localDbInstanceName);

                return(newInstance);
            }

            // Order the version numbers so we try highest version to lowest version.
            IEnumerable <string> orderedVersionNumbers =
                allowedLocalDbVersions.OrderByDescending(version => version);

            IEnumerable <string> installedVersions = localDbApi.Versions;

            foreach (string versionNumber in orderedVersionNumbers)
            {
                if (installedVersions.Contains(versionNumber))
                {
                    localDbProvider.Version = versionNumber;

                    SqlLocalDbInstance newInstance = localDbProvider.CreateInstance(localDbInstanceName);

                    return(newInstance);
                }
            }

            string errorMessage =
                $@"DbTestMonkey was unable to find an appropriate LocalDb version to use. The following versions were configured to be considered in the app.config by you:
   {string.Join(",", allowedLocalDbVersions.Select(s => "\"" + s + "\""))}
However only the following LocalDb versions were found to be installed:
   {string.Join(",", installedVersions.Select(s => "\"" + s + "\""))}
Please correct this error by one of the following options:
 * Add an installed version of LocalDb into your projects app.config <allowedLocalDbVersions> element. You can find the installed versions by running ""sqllocaldb versions"" at the command line.
 * Remove the <allowedLocalDbVersions> element from your app.config which means DbTestMonkey will use the latest installed version of LocalDb on your machine.
 * Install a version of LocalDb that is configured in your app.config <allowedLocalDbVersions> element.";

            throw new InvalidOperationException(errorMessage);
        }
Example #3
0
        public LocalDb(string tempBasePath = @"C:\temp\", string instanceName = "v11.0")
        {
            InstanceName  = instanceName;
            _tempBasePath = tempBasePath;

            ISqlLocalDbApi localDb = new SqlLocalDbApiWrapper();

            if (!localDb.IsLocalDBInstalled())
            {
                throw new SqlLocalDbException("LocalDb is not installed.");
            }

            CreateDirectoriesIfNotExists();

            DbName            = RandomName();
            _attachDbFileName = Path.Combine(_tempBasePath, DbName) + ".mdf";
        }
Example #4
0
        private bool disposedValue = false; // To detect redundant calls

        protected void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects).
                    // Optional.
                    // By default, the next test class which uses TestignSiloHost will
                    // cause a fresh Orleans silo environment to be created.
                    this.HostedCluster.StopAllSilos();

                    instance.Stop();
                    ISqlLocalDbApi localDB = new SqlLocalDbApiWrapper();
                    localDB.DeleteInstance(instance.Name);
                }

                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                // TODO: set large fields to null.

                disposedValue = true;
            }
        }
        private void SetupLocalDbInstance(ProviderConfiguration config)
        {
            var localDbApi = new SqlLocalDbApiWrapper();

            if (!localDbApi.IsLocalDBInstalled())
            {
                throw new InvalidOperationException("LocalDB is not installed on this machine.");
            }

            if (!string.IsNullOrWhiteSpace(config.LocalDbInstanceName))
            {
                LogAction("Preparing LocalDb instance: " + config.LocalDbInstanceName);

                ISqlLocalDbInstance localDbInstance =
                    GetAppropriateLocalDbInstance(
                        localDbApi,
                        config.LocalDbInstanceName,
                        config.Versions
                        .Cast <LocalDbAllowedVersion>()
                        .Select(v => v.Version));

                localDbInstance.Start();
            }
            else if (!string.IsNullOrWhiteSpace(config.ConnectionString))
            {
                LogAction("LocalDb instance name was not provided so we must assume it is already set up.");

                // SQL Server instance must already be set up.
                return;
            }
            else
            {
                throw new InvalidOperationException(
                          "IsLocalDbInstance was true in configuration but no instance name or connection string was configured.");
            }
        }
Example #6
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            ISqlLocalDbApi localDB = new SqlLocalDbApiWrapper();

            if (!localDB.IsLocalDBInstalled())
            {
                MessageBox.Show("Máy tính của bạn phải cài đặt SQL Server LocalDB hoặc phiên bản Express để có thể sử dụng phần mềm.");
                return;
            }

            string uuid = "nhom7-928cf731-171f-464f-aa55-24e814eeb224";

            ISqlLocalDbProvider provider = new SqlLocalDbProvider();

            IList <ISqlLocalDbInstanceInfo> instances = provider.GetInstances();

            bool flag = false;

            foreach (ISqlLocalDbInstanceInfo instanceInfo in instances)
            {
                if (instanceInfo.Name == uuid)
                {
                    flag = true;
                    break;
                }
            }

            if (!flag)
            {
                WaitForm frm = new WaitForm();
                frm.Show();
                ISqlLocalDbInstance instance = provider.CreateInstance(uuid);
                instance.Start();

                try
                {
                    if (IsCurrentUserAdmin())
                    {
                    }
                    try
                    {
                        using (SqlConnection connection = instance.CreateConnection())
                        {
                            connection.Open();
                            try
                            {
                                string   scriptText   = Resources.script.ToString();
                                string[] splitter     = new string[] { "\r\nGO\r\n" };
                                string[] commandTexts = scriptText.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
                                foreach (string commandText in commandTexts)
                                {
                                    using (SqlCommand command = new SqlCommand(commandText, connection))
                                    {
                                        command.ExecuteNonQuery();
                                    }
                                }
                            }
                            finally
                            {
                                connection.Close();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                    finally
                    {
                        if (IsCurrentUserAdmin())
                        {
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                finally
                {
                    instance.Stop();
                }

                frm.Close();
            }
            else
            {
                ISqlLocalDbInstance instance = provider.GetInstance(uuid);
            }

            Global.connstring = @"Data Source=(LocalDB)\nhom7-928cf731-171f-464f-aa55-24e814eeb224;Initial Catalog=QUANLYTHUVIEN_Nhom7_14521100_15520048_15520062_15520115;Integrated Security=True";

            Application.Run(new frmDangNhap());
        }