Пример #1
0
        private SqlLocalDbInstance GetExistingInstance(SqlLocalDbProvider localDbProvider, string localDbInstanceName)
        {
            try
            {
                SqlLocalDbInstance existingInstance = localDbProvider.GetInstance(localDbInstanceName);

                IEnumerable <string> filePaths = GetPhysicalFilesForServer(existingInstance);

                bool isMissingFiles = filePaths.Any(path => !File.Exists(path));

                // If at least one file doesn't exist, delete them all.
                // We have found that the SqlLocalDbInstanceInfo.Exists property to not be reliable.
                if (isMissingFiles)
                {
                    LogAction("Existing LocalDb instance with name " + localDbInstanceName + " was found but some physical files were missing");
                    LogAction("Deleting instance and will attempt to recreate");

                    existingInstance.Stop();
                    SqlLocalDbApi.DeleteInstance(existingInstance.Name, deleteFiles: true);

                    foreach (string filePath in filePaths)
                    {
                        try
                        {
                            File.Delete(filePath);
                        }
                        catch (DirectoryNotFoundException) { }
                    }

                    return(null);
                }
                else
                {
                    LogAction("Found existing LocalDb instance with name " + localDbInstanceName + " and will use it");
                    LogAction("If you would like to delete this existing instance and let DbTestMonkey create a new instance run the following commands at a command line");
                    LogAction("   sqllocaldb stop " + localDbInstanceName);
                    LogAction("   sqllocaldb delete " + localDbInstanceName);

                    return(existingInstance);
                }
            }
            catch (InvalidOperationException)
            {
                LogAction("Existing LocalDb instance with name " + localDbInstanceName + " was not found");
            }

            return(null);
        }
Пример #2
0
        private void CleanupLocalDbDatabase()
        {
            SqlLocalDbApi.AutomaticallyDeleteInstanceFiles = true;
            SqlLocalDbApi.StopOptions = StopInstanceOptions.KillProcess;

            var localDbProvider     = new SqlLocalDbProvider();
            var localDbInstanceInfo = localDbProvider.GetInstances().FirstOrDefault(instance => instance.Name == DatabaseName);

            if (localDbInstanceInfo != null)
            {
                var localDbInstance = localDbProvider.GetInstance(DatabaseName);
                if (!localDbInstance.IsRunning)
                {
                    localDbInstance.Start();
                }
                this.CleanupMsSqlDatabase(localDbInstance.CreateConnectionStringBuilder().ConnectionString);
                SqlLocalDbApi.StopInstance(DatabaseName, TimeSpan.FromSeconds(20.0));
                SqlLocalDbApi.DeleteInstance(DatabaseName);
            }
        }
Пример #3
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());
        }
Пример #4
0
        /// <summary>
        /// Load all github tags in the background.
        /// </summary>
        private void LoadData()
        {
            //
            // Initialize the LocalDb service only once.
            //
            if (localDb == null)
            {
                var provider = new SqlLocalDbProvider();
                SqlLocalDbInstance instance;

                try
                {
                    //
                    // If we find an existing instance then shut it down and delete it.
                    //
                    instance = provider.GetInstance("RockLauncher");
                    if (instance.IsRunning)
                    {
                        instance.Stop();
                    }
                    SqlLocalDbInstance.Delete(instance);
                }
                finally
                {
                    //
                    // Create a new instance and keep a reference to it.
                    //
                    localDb = provider.CreateInstance("RockLauncher");
                    localDb.Start();
                }
            }

            //
            // Load all the instances from the file system.
            //
            var instances = Directory.GetDirectories(Support.GetInstancesPath())
                            .Select(d => Path.GetFileName(d)).ToList();

            //
            // Convert pre-1.0 instance folders to 1.0 instance folders, which contain a
            // RockWeb for the current instance data.
            //
            foreach (string instance in instances)
            {
                string instancePath = Path.Combine(Support.GetInstancesPath(), instance);
                string rockwebPath  = Path.Combine(instancePath, "RockWeb");

                if (!Directory.Exists(rockwebPath))
                {
                    Directory.CreateDirectory(rockwebPath);

                    foreach (var d in Directory.GetDirectories(instancePath))
                    {
                        if (!Path.GetFileName(d).Equals("RockWeb", StringComparison.CurrentCultureIgnoreCase))
                        {
                            Directory.Move(d, Path.Combine(rockwebPath, Path.GetFileName(d)));
                        }
                    }

                    foreach (var f in Directory.GetFiles(instancePath))
                    {
                        Directory.Move(f, Path.Combine(rockwebPath, Path.GetFileName(f)));
                    }
                }
            }

            //
            // Update the UI with the new list of instances.
            //
            Dispatcher.Invoke(() =>
            {
                cbInstances.ItemsSource = instances;
                if (instances.Count > 0)
                {
                    cbInstances.SelectedIndex = 0;
                }

                txtStatus.Text = "Idle";

                UpdateState();
            });
        }