Пример #1
0
        private void _buttonNewDatabase_Click(object sender, RoutedEventArgs e)
        {
            OpenControls.Wpf.Serialisation.RegistryItemSerialiser        registryItemSerialiser = new OpenControls.Wpf.Serialisation.RegistryItemSerialiser(_keyPath);
            OpenControls.Wpf.DatabaseDialogs.Model.DatabaseConfiguration databaseConfiguration  = new OpenControls.Wpf.DatabaseDialogs.Model.DatabaseConfiguration(registryItemSerialiser);
            if (registryItemSerialiser.OpenKey())
            {
                databaseConfiguration.Load();
            }

            OpenControls.Wpf.DatabaseDialogs.ViewModel.NewDatabaseViewModel newDatabaseViewModel = new OpenControls.Wpf.DatabaseDialogs.ViewModel.NewDatabaseViewModel(databaseConfiguration);
            OpenControls.Wpf.DatabaseDialogs.View.NewDatabaseView           newDatabaseView      =
                new OpenControls.Wpf.DatabaseDialogs.View.NewDatabaseView(new OpenControls.Wpf.DatabaseDialogs.Model.Encryption());
            newDatabaseView.DataContext = newDatabaseViewModel;
            if (newDatabaseView.ShowDialog() != true)
            {
                return;
            }
            if (!registryItemSerialiser.IsOpen)
            {
                registryItemSerialiser.CreateKey();
            }
            databaseConfiguration.Save();
            registryItemSerialiser.Close();

            try
            {
                OpenControls.Wpf.DatabaseDialogs.Model.DatabaseProvider databaseProvider = (OpenControls.Wpf.DatabaseDialogs.Model.DatabaseProvider)newDatabaseViewModel.SelectedDatabaseProvider;
                if (databaseProvider == OpenControls.Wpf.DatabaseDialogs.Model.DatabaseProvider.SQLite)
                {
                    SQLiteDatabase.CreateDatabase(this, newDatabaseViewModel.SQLite_Folder, newDatabaseViewModel.SQLite_DatabaseName);
                    IDatabaseHost.IConfigurationStore = new DBStore.SQLiteConfigurationStore(IDatabaseHost.Database);
                    IDatabaseHost.ISpeciesStore       = new DBStore.SQLiteSpeciesStore(IDatabaseHost.Database);
                    IDatabaseHost.IImagePathsStore    = new DBStore.SQLiteImagePathsStore(IDatabaseHost.Database);
                    IDatabaseHost.IImageStore         = new DBStore.SQLiteImageStore(IDatabaseHost.Database);
                }
                else if (databaseProvider == OpenControls.Wpf.DatabaseDialogs.Model.DatabaseProvider.MicrosoftSQLServer)
                {
                    string server =
                        (newDatabaseViewModel.SqlServerInstances.Count > 0) ?
                        newDatabaseViewModel.SqlServerInstances[newDatabaseViewModel.SelectedSqlServerInstance] :
                        null;
                    SQLServerDatabase.CreateDatabase(
                        this,
                        server,
                        newDatabaseViewModel.SQLServer_UseWindowsAuthentication,
                        newDatabaseViewModel.SQLServer_UserName,
                        newDatabaseViewModel.SQLServer_Password,
                        newDatabaseViewModel.SQLServer_Folder,
                        newDatabaseViewModel.SQLServer_Filename);
                    IDatabaseHost.IConfigurationStore = new DBStore.SQLServerConfigurationStore(IDatabaseHost.Database);
                    IDatabaseHost.ISpeciesStore       = new DBStore.SQLServerSpeciesStore(IDatabaseHost.Database);
                    IDatabaseHost.IImagePathsStore    = new DBStore.SQLServerImagePathsStore(IDatabaseHost.Database);
                    IDatabaseHost.IImageStore         = new DBStore.SQLServerImageStore(IDatabaseHost.Database);
                }
                else if (databaseProvider == OpenControls.Wpf.DatabaseDialogs.Model.DatabaseProvider.PostGreSQL)
                {
                    PostgreSQLDatabase.CreateDatabase(this, newDatabaseViewModel.PostgreSQL_IPAddress, newDatabaseViewModel.PostgreSQL_Port, newDatabaseViewModel.PostgreSQL_UseWindowsAuthentication, newDatabaseViewModel.PostgreSQL_UserName, newDatabaseViewModel.PostgreSQL_Password, newDatabaseViewModel.PostgreSQL_DatabaseName);
                    IDatabaseHost.IConfigurationStore = new DBStore.PostgreSQLConfigurationStore(IDatabaseHost.Database);
                    IDatabaseHost.ISpeciesStore       = new DBStore.PostgreSQLSpeciesStore(IDatabaseHost.Database);
                    IDatabaseHost.IImagePathsStore    = new DBStore.PostgreSQLImagePathsStore(IDatabaseHost.Database);
                    IDatabaseHost.IImageStore         = new DBStore.PostgreSQLImageStore(IDatabaseHost.Database);
                }
                else if (databaseProvider == OpenControls.Wpf.DatabaseDialogs.Model.DatabaseProvider.MySQL)
                {
                    MySQLDatabase.CreateDatabase(this, newDatabaseViewModel.MySQL_IPAddress, newDatabaseViewModel.MySQL_Port, newDatabaseViewModel.MySQL_UseWindowsAuthentication, newDatabaseViewModel.MySQL_UserName, newDatabaseViewModel.MySQL_Password, newDatabaseViewModel.MySQL_DatabaseName);
                    IDatabaseHost.IConfigurationStore = new DBStore.MySQLConfigurationStore(IDatabaseHost.Database);
                    IDatabaseHost.ISpeciesStore       = new DBStore.MySQLSpeciesStore(IDatabaseHost.Database);
                    IDatabaseHost.IImagePathsStore    = new DBStore.MySQLImagePathsStore(IDatabaseHost.Database);
                    IDatabaseHost.IImageStore         = new DBStore.MySQLImageStore(IDatabaseHost.Database);
                }
                else
                {
                    throw new Exception("Unsupported database type");
                }

                IDatabaseHost.IConfigurationStore.CreateTable();
                IDatabaseHost.IConfigurationStore.Initialise();
                IDatabaseHost.ISpeciesStore.CreateTable();
                IDatabaseHost.IImagePathsStore.CreateTable();
                IDatabaseHost.IImageStore.CreateTable();
            }
            catch (Exception exception)
            {
                System.Windows.Forms.MessageBox.Show(exception.Message);
                return;
            }

            ShowSpeciesListView(IDatabaseHost.Database);
        }