Esempio n. 1
0
        private ISQLConnectionSettings CheckSQLiteDB(ISQLConnectionSettings tmpSettings)
        {
            ISQLConnectionSettings newSettings = new SQLConnectionSettings(tmpSettings);

            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                newSettings.Database = ofd.OpenFileDialogReturnPath();
                newSettings.Name     = "currentDB";
                if (!string.IsNullOrWhiteSpace(newSettings.Database))
                {
                    newSettings = CheckSchemaSQLite(newSettings);
                }
                else
                {
                    DialogResult dialog = MessageBox.Show("Create new DB?", "DB is empty!", MessageBoxButtons.YesNo);

                    if (dialog == DialogResult.Yes)
                    {
                        newSettings.Database = "MainDB.db";
                        SQLiteDBOperations connector = new SQLiteDBOperations(newSettings);
                        connector.TryMakeLocalDB();
                        newSettings = CheckSchemaSQLite(newSettings);
                    }
                }
            }
            return(newSettings);
        }
Esempio n. 2
0
        async Task ImportFileIntoLocalDB(string filePath, ImportedFileType fileType)
        {
            readerModelCommon = new FileReaderModels();

            ISQLConnectionSettings newConnection = new SQLConnectionSettings
            {
                Database = $"{Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath))}.db"
            };

            dBOperations = new SQLiteModelDBOperations(newConnection);
            dBOperations.EvntInfoMessage += new SqlAbstractConnector.Message <TextEventArgs>(AddLineAtTextBoxResultShow);

            SetAdminStatusLabelText($"Импорт файла '{filePath}' в базу данных...");
            AddLineAtTextBoxResultShow();

            readerModelCommon.EvntInfoMessage    += new FileReaderModels.Message <TextEventArgs>(AddLineAtTextBoxResultShow);
            readerModelCommon.EvntHeaderReady    += new FileReaderModels.CollectionFull <BoolEventArgs>(ModelCommon_EvntHeaderReady);
            readerModelCommon.EvntCollectionFull += new FileReaderModels.CollectionFull <BoolEventArgs>(ModelCommon_collectionFull);

            await readerModelCommon.SelectImportingMethod(filePath, fileType, MAX_ELEMENTS_COLLECTION);

            readerModelCommon.EvntCollectionFull -= ModelCommon_collectionFull;
            readerModelCommon.EvntHeaderReady    -= ModelCommon_EvntHeaderReady;
            readerModelCommon.EvntInfoMessage    -= AddLineAtTextBoxResultShow;

            SetAdminStatusLabelText($"Всего в БД '{newConnection.Database}' импортировано {readerModelCommon.importedRows} строк.");
            AddLineAtTextBoxResultShow("newConnection: " + newConnection.ToString());
            dBOperations.EvntInfoMessage -= AddLineAtTextBoxResultShow;

            readerModelCommon = null;
            AddLineAtTextBoxResultShow();

            currentSQLConnectionStore.Set(newConnection);
            currentSQLConnectionStore.Refresh();
        }
Esempio n. 3
0
        private ISQLConnectionSettings CheckSQLiteDB()
        {
            string db = null;

            using (OpenFileDialog ofd = new OpenFileDialog())
            { db = ofd.OpenFileDialogReturnPath(Properties.Resources.DialogDbFile, "Выберите файл с БД SQLite:"); }

            ISQLConnectionSettings newSettings = new SQLConnectionSettings
            {
                Database     = db,
                ProviderName = SQLProvider.SQLite
            };

            if (!string.IsNullOrWhiteSpace(newSettings.Database) && File.Exists(newSettings.Database))
            {
                DbSchema schemaDb = null;

                try
                {
                    schemaDb = DbSchema.LoadDB(newSettings.Database);
                    IList <string> tablesDb = schemaDb.Tables.Select(tbl => tbl.Value.TableName).ToList();

                    if (tablesDb?.Count > 0)
                    {
                        newSettings.Table = tablesDb[0];

                        cmbDataBases.DataSource = new List <string> {
                            newSettings.Database
                        };
                        cmbTables.DataSource = tablesDb;
                        tbResultShow.AppendLine("The inputed data are correct.");
                    }
                }
                catch (Exception e)
                {
                    newSettings = null;
                    tbResultShow.AppendLine($"Ошибка в БД: {e.Message}");
                    tbResultShow.AppendLine($"{e}");
                }
                finally
                {
                    if (schemaDb?.Tables?.Count == 0)
                    {
                        newSettings = null;
                        tbResultShow.AppendLine("Подключенная база данных пустая или же в ней отсутствуют какие-либо таблицы с данными!");
                        tbResultShow.AppendLine("Предварительно создайте базу данных, таблицы и импортируйте/добавьте в них данные...");
                        tbResultShow.AppendLine("Заблокирован функционал по получению данных из таблиц...");
                    }
                }
            }

            return(newSettings);
        }
Esempio n. 4
0
        public static ISQLConnectionSettings ToSQLConnectionSettings(this SQLConnectionData data)
        {
            ISQLConnectionSettings newSettings = new SQLConnectionSettings();

            newSettings.Host         = data.Host;
            newSettings.Database     = data.Database;
            newSettings.Name         = data.Name;
            newSettings.Port         = data.Port;
            newSettings.Table        = data.Table;
            newSettings.Username     = data.Username;
            newSettings.Password     = data.Password;
            newSettings.ProviderName = data.ProviderName;

            return(newSettings);
        }
Esempio n. 5
0
        private ISQLConnectionSettings CheckSchemaSQLite(ISQLConnectionSettings tmpSettings)
        {
            ISQLConnectionSettings newSettings = new SQLConnectionSettings(tmpSettings);

            DbSchema       schemaDB = null;
            IList <string> tablesDB;
            DBModel        db = new DBModel();

            try
            {
                schemaDB = DbSchema.LoadDB(tmpSettings.Database);
                tablesDB = new List <string>();

                foreach (var tbl in schemaDB.Tables)
                {
                    tablesDB.Add(tbl.Value.TableName);
                }

                if (tablesDB?.Count > 0)
                {
                    newSettings.Table        = tablesDB[0];
                    newSettings.ProviderName = SQLProvider.SQLite;

                    cmbDataBases.DataSource = new List <string>()
                    {
                        tmpSettings.Database
                    };
                    cmbTables.DataSource = tablesDB;

                    tbResultShow.AppendLine("The inputed data are correct.");
                }
            }
            catch (Exception e)
            {
                tbResultShow.AppendLine($"Ошибка в БД: {e.Message}");
                tbResultShow.AppendLine($"{e.ToString()}");
            }
            finally
            {
                if (schemaDB?.Tables?.Count == 0)
                {
                    tbResultShow.AppendLine("Подключенная база данных пустая или же в ней отсутствуют какие-либо таблицы с данными!");
                    tbResultShow.AppendLine("Предварительно создайте базу данных, таблицы и импортируйте/добавьте в них данные...");
                    tbResultShow.AppendLine("Заблокирован функционал по получению данных из таблиц...");
                }
            }
            return(newSettings);
        }
Esempio n. 6
0
        public static ISQLConnectionSettings ToISQLConnectionSettings(this IDictionary <string, object> config)
        {
            ISQLConnectionSettings data = new SQLConnectionSettings();

            if (!(config?.Count > 0))
            {
                return(data);
            }

            data.Database     = config[nameof(ISQLConnectionSettings.Database)]?.ToString();
            data.Table        = config[nameof(ISQLConnectionSettings.Table)]?.ToString();
            data.ProviderName = config[nameof(ISQLConnectionSettings.ProviderName)]?.ToString().GetSQLProvider();
            data.Host         = config[nameof(ISQLConnectionSettings.Host)]?.ToString();
            data.Port         = int.TryParse(config[nameof(ISQLConnectionSettings.Port)]?.ToString(), out int port) ? port : 0;
            data.Username     = config[nameof(ISQLConnectionSettings.Username)]?.ToString();
            data.Password     = config[nameof(ISQLConnectionSettings.Password)]?.ToString();
            return(data);
        }
Esempio n. 7
0
        private ISQLConnectionSettings CheckMySQLDB(ISQLConnectionSettings tmpSettings)
        {
            ISQLConnectionSettings newSettings = new SQLConnectionSettings(tmpSettings);

            newSettings.Host     = tbHost?.Text;
            newSettings.Port     = int.TryParse(tbPort?.Text, out int port) ? port : 0;
            newSettings.Database = cmbDataBases?.SelectedItem?.ToString() ?? selectedDB;
            newSettings.Username = tbUserName?.Text;
            newSettings.Password = tbPassword?.Text;
            MySQLUtils mySQL = new MySQLUtils(newSettings);

            try
            {
                using (DataTable dt = mySQL.GetTable("SHOW DATABASES"))
                {
                    IList <string> list = new List <string>();
                    foreach (DataRow r in dt.Rows)
                    {
                        list.Add(r[0].ToString());
                    }

                    if (list?.Count > 0)
                    {
                        cmbDataBases.DataSource = list;
                        cmbDataBases.Enabled    = true;
                        newSettings.Database    = list[0];

                        tbResultShow.AppendLine("The inputed data are correct.");
                    }
                }
            }
            catch (MySqlException excpt)
            {
                tbResultShow.AppendLine("My SQL error - " + excpt.Message + ":");
                tbResultShow.AppendLine(excpt.ToString());
            }
            catch (Exception excpt)
            {
                tbResultShow.AppendLine(excpt.Message + ":");
                tbResultShow.AppendLine(excpt.ToString());
            }

            return(newSettings);
        }
Esempio n. 8
0
        public static ISQLConnectionSettings ToSQLConnectionSettings(this IList <RegistryEntity> entities)
        {
            ISQLConnectionSettings connectionSettings = new SQLConnectionSettings();

            if (!(entities?.Count > 0))
            {
                return(connectionSettings);
            }

            foreach (var entity in entities)
            {
                string key = entity?.Key;
                switch (key)
                {
                case "Name":
                {
                    connectionSettings.Name = entity?.Value.ToString();
                    break;
                }

                case "ProviderName":
                {
                    connectionSettings.ProviderName = entity?.Value.ToString().GetSQLProvider();
                    break;
                }

                case "Host":
                {
                    connectionSettings.Host = entity?.Value.ToString();
                    break;
                }

                case "Port":
                {
                    connectionSettings.Port = int.TryParse(entity?.Value.ToString(), out int port) ? port : 0;
                    break;
                }

                case "Username":
                {
                    connectionSettings.Username = entity?.Value.ToString();
                    break;
                }

                case "Password":
                {
                    connectionSettings.Password = entity?.Value.ToString();
                    break;
                }

                case "Database":
                {
                    connectionSettings.Database = entity?.Value.ToString();
                    break;
                }

                case "Table":
                {
                    connectionSettings.Table = entity?.Value.ToString();
                    break;
                }

                default:
                {
                    break;
                }
                }
            }

            return(connectionSettings);
        }