Exemplo n.º 1
0
        /// <summary>
        /// Open the specified database file
        /// </summary>
        /// <param name="databaseFile">Database file name to open</param>
        /// <param name="password">Password of database file</param>
        public bool Open(string databaseFile, string password)
        {
            Close();
            FileName = Path.GetFullPath(databaseFile);
            Password = password;

            bool    ok      = false;
            Version version = GetSdfVersion(FileName);

            if (version == null)
            {
                foreach (Version ver in AvailableVersions.Reverse <Version>())
                {
                    ok = OpenConnection(ver, databaseFile, password);
                    if (ok || BadPassword)
                    {
                        break;
                    }
                }
            }
            else
            {
                ok = OpenConnection(version, databaseFile, password);
                if (!ok && (!BadPassword || password != null))
                {
                    GlobalText.ShowError("UnableToOpen", LastError);
                }
            }
            return(ok);
        }
Exemplo n.º 2
0
        private void btUpgrade_Click(object sender, EventArgs e)
        {
            if (!CreateBackup())
            {
                return;
            }

            bool ok = false;

            this.Cursor = Cursors.WaitCursor;
            try
            {
                ok = db.Upgrade(tbFileName.Text, openPassword, tbPassword.Text.Trim(), (Version)cbUpgradeTo.SelectedItem);
                if (ok)
                {
                    GlobalText.ShowInfo("UpgradeDone");
                }
                else
                {
                    GlobalText.ShowError("UpgradeError", db.LastError);
                }
            }
            catch (Exception ex)
            {
                GlobalText.ShowError("UpgradeError", ex.Message);
            }
            this.Cursor = Cursors.Default;

            if (ok)
            {
                LoadDatabase(tbFileName.Text, tbPassword.Text.Trim());
            }
            SetActiveButtons();
        }
Exemplo n.º 3
0
        private void btVerify_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                if (db.Verify(tbFileName.Text, tbPassword.Text.Trim()))
                {
                    GlobalText.ShowInfo("VerifyDone");
                }
                else if (db.LastError == "")
                {
                    GlobalText.ShowError("VerifyFault");
                }
                else
                {
                    GlobalText.ShowError("VerifyError", db.LastError);
                }
            }
            catch (Exception ex)
            {
                GlobalText.ShowError("VerifyError", ex.Message);
            }
            db.Close();
            this.Cursor = Cursors.Default;

            LoadDatabase(tbFileName.Text, tbPassword.Text.Trim());
        }
Exemplo n.º 4
0
        private bool CreateBackup()
        {
            if (string.IsNullOrEmpty(db.FileName))
            {
                GlobalText.ShowError("NoDatabaseSelect");
                return(false);
            }

            if (!File.Exists(db.FileName))
            {
                GlobalText.ShowError("DatabaseMissing");
                return(false);
            }

            string backupFile = "";
            int    i          = 1;

            try
            {
                while (File.Exists(backupFile = Path.ChangeExtension(db.FileName, "." + i.ToString("0000") + Path.GetExtension(db.FileName))))
                {
                    i++;
                }
                File.Copy(db.FileName, backupFile);
                return(true);
            }
            catch (Exception ex)
            {
                GlobalText.ShowError("BackupError", ex.Message);
                return(false);
            }
        }
Exemplo n.º 5
0
        private void btShrink_Click(object sender, EventArgs e)
        {
            if (!CreateBackup())
            {
                return;
            }

            bool ok = false;

            Cursor = Cursors.WaitCursor;
            try
            {
                ok = db.Shrink(tbFileName.Text, openPassword, tbPassword.Text.Trim());
                if (ok)
                {
                    GlobalText.ShowInfo("ShrinkDone");
                }
                else
                {
                    GlobalText.ShowError("ShrinkError", db.LastError);
                }
            }
            catch (Exception ex)
            {
                GlobalText.ShowError("ShrinkError", ex.Message);
            }
            Cursor = Cursors.Default;

            if (ok)
            {
                LoadDatabase(tbFileName.Text, tbPassword.Text.Trim());
            }
            SetActiveButtons();
        }
Exemplo n.º 6
0
        private void btCreate_Click(object sender, EventArgs e)
        {
            bool ok = false;

            Cursor = Cursors.WaitCursor;
            try
            {
                ok = db.CreateDatabase(tbFileName.Text, tbPassword.Text.Trim(), (Version)cbVersion.SelectedItem);
                if (ok)
                {
                    GlobalText.ShowInfo("CreateDone");
                }
                else
                {
                    GlobalText.ShowError("CreateError", db.LastError);
                }
            }
            catch (Exception ex)
            {
                GlobalText.ShowError("CreateError", ex.Message);
            }
            Cursor = Cursors.Default;

            if (ok)
            {
                LoadDatabase(tbFileName.Text, tbPassword.Text.Trim());
            }
            SetActiveButtons();
        }
Exemplo n.º 7
0
        /// <summary>
        /// Load database with password
        /// </summary>
        /// <param name="fileName">Database file name</param>
        /// <param name="password">Database password</param>
        private void LoadDatabase(string fileName, string password)
        {
            Reset();

            try
            {
                if (!File.Exists(fileName))
                {
                    throw new Exception($"{GlobalText.GetValue("FileNotFound")}: '{fileName}'");
                }

                Cursor = Cursors.WaitCursor;

                if (db.Open(fileName, password))
                {
                    Text = string.Concat(formText, " - ", db.FileName);

                    // Fill tree with database name and table names
                    treeDb.BeginUpdate();
                    treeDb.Nodes.Clear();
                    TreeNode main = treeDb.Nodes.Add("Database", Path.GetFileNameWithoutExtension(fileName), 0, 0);
                    foreach (string tableName in db.TableNames)
                    {
                        main.Nodes.Add(tableName, tableName, 1, 1);
                    }
                    main.Expand();
                    treeDb.EndUpdate();
                    treeDb.SelectedNode = treeDb.Nodes[0];
                    settings.AddToRecentFiles(fileName);
                    UpdateRecentFilesMenu();
                }
                else
                {
                    bool badPassword = db.BadPassword;
                    Reset();
                    if (badPassword)
                    {
                        var form = new GetPassForm();
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            LoadDatabase(fileName, form.edPass.Text.Trim());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                GlobalText.ShowError("UnableToOpen", ex.Message);
                btnQuery.Enabled = btnExecute.Enabled = btnClear.Enabled = false;
                settings.RemoveFromRecentFiles(fileName);
                UpdateRecentFilesMenu();
            }
            Cursor = Cursors.Default;

            UpdateStatus();
        }
Exemplo n.º 8
0
 private void dataGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
 {
     // Error when show/edit field
     GlobalText.ShowError("ChangingDataError", e.Exception.Message);
     e.Cancel = true;
 }
Exemplo n.º 9
0
        private void FillTreeDb(string fileName)
        {
            bool   schema   = false;
            bool   data     = false;
            bool   ok       = false;
            string cmdError = null;

            treeDb.Nodes.Clear();
            treeDb.BeginUpdate();
            var reader = new StreamReader(fileName);

            ddl = new List <string>();
            // Regular expression to search texts finished with semicolons that is not between single quotes
            for (Match m = Regex.Match(reader.ReadToEnd(), @"(?:[^;']|'[^']*')+;\s*", RegexOptions.Compiled); m.Success; m = m.NextMatch())
            {
                ddl.Add(m.Value.TrimEnd('\r', '\n'));
            }

            RegexOptions options          = RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase;
            var          regexCreateTable = new Regex(@"(?<=^CREATE\s+TABLE\s+\[)[^\]]*(?=\]\s+\([^\)]*\))", options);
            var          regexInsert      = new Regex(@"(?<=^INSERT\s+INTO\s+\[)[^\]]*(?=\]\s+\([^\)]*\)\s+VALUES\s+\([^\)]*\))", options);
            var          regexAlterTable  = new Regex(@"^ALTER\s+TABLE\s+\[", options);
            var          regexCreateIndex = new Regex(@"^CREATE\s+.*INDEX\s+", options);
            var          regexSetIdentity = new Regex(@"^SET\s+IDENTITY_INSERT\s+\[", options);

            for (int i = 0; i < ddl.Count; i++)
            {
                ok = false;
                Match match1 = regexCreateTable.Match(ddl[i]);
                if (match1.Success)
                {
                    treeDb.Nodes.Add(match1.Value, match1.Value, 0, 0).Checked = true;
                    schema = true;
                    ok     = true;
                }
                Match match2 = regexInsert.Match(ddl[i]);
                if (match2.Success)
                {
                    if (!treeDb.Nodes.ContainsKey(match2.Value))
                    {
                        treeDb.Nodes.Add(match2.Value, match2.Value, 0, 0).Checked = true;
                    }
                    data = true;
                    ok   = true;
                }
                if (!ok)
                {
                    if (regexAlterTable.IsMatch(ddl[i]) | regexCreateIndex.IsMatch(ddl[i]) | regexSetIdentity.IsMatch(ddl[i]))
                    {
                        ok = true;
                    }
                    if (!ok)
                    {
                        cmdError = ddl[i].Length <= 500 ? ddl[i] : $"{ddl[i].Remove(500)}...";
                        break;
                    }
                }
            }

            treeDb.EndUpdate();
            treeDb.ExpandAll();
            cbSchema.Enabled = cbSchema.Checked = schema;
            cbData.Enabled   = cbData.Checked = data;

            if (!ok)
            {
                GlobalText.ShowError("ImportFormatError", cmdError);
                abort = true;
            }
        }
Exemplo n.º 10
0
        private void btnImport_Click(object sender, EventArgs e)
        {
            bool anyTableExists  = false;
            bool allTablesExists = true;

            foreach (TreeNode node in treeDb.Nodes)
            {
                if (node.Checked)
                {
                    bool exists = db.TableNames.Contains(node.Text, StringComparer.InvariantCultureIgnoreCase);
                    if (exists)
                    {
                        anyTableExists = true;
                    }
                    else
                    {
                        allTablesExists = false;
                    }
                }
            }

            if (cbSchema.Checked && anyTableExists)
            {
                GlobalText.ShowError("ErrorImportingTablesSchema");
                DialogResult = DialogResult.Abort;
                return;
            }

            if (cbData.Checked && !cbSchema.Checked && !allTablesExists)
            {
                GlobalText.ShowError("ErrorImportingTablesData");
                DialogResult = DialogResult.Abort;
                return;
            }

            FilterDdl();

            toolStripProgressBar1.Value   = 0;
            toolStripProgressBar1.Maximum = ddl.Count;
            DbCommand cmd = db.Connection.CreateCommand();

            cmd.CommandType = CommandType.Text;

            try
            {
                foreach (string sql in ddl)
                {
                    toolStripProgressBar1.Increment(1);
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                }
                GlobalText.ShowInfo("ImportOk");
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                string sql = cmd.CommandText.Length <= 500 ? cmd.CommandText : $"{cmd.CommandText.Remove(500)}...";
                GlobalText.ShowError("ImportError", ex.Message + Environment.NewLine + Environment.NewLine + sql);
                DialogResult = DialogResult.Abort;
            }
        }