private void AddRuleButton_Click(object sender, EventArgs e) { includes = includesTextBox.Text.Trim(); path = pathTextBox.Text.Trim(); extension = ExtensionTextBox.Text.Trim(); isCaseSensetive = isCaseSensetiveCheckBox.Checked ? 1 : 0; // Since SQLite doesn't have native boolean, I will use digits instead if (extension == "" || path == "") { messageLabelFunc(resourceManager.GetString("ReqFieldsAreEmpty"), Color.DarkOrange); return; } if (extension[0] != '.') { extension = '.' + extension; } if (!extValidate(extension) || !pathValidate(path)) { return; } if (DBConnection.IsRuleExist(extension, includes, isCaseSensetive)) { messageLabelFunc(string.Format(resourceManager.GetString("ruleForExtAlreadyExist"), extension), Color.DarkOrange); return; } RuleModel rule = new RuleModel { ruleExtension = extension, ruleIncludes = includes, ruleIsCaseSensetive = isCaseSensetive, rulePath = path }; DBConnection.AddRule(rule); messageLabelFunc(string.Format(resourceManager.GetString("ruleCreatedSuccessfuly")), Color.Lime); ExtensionTextBox.Text = ""; includesTextBox.Text = ""; pathTextBox.Text = ""; GridUpdater gu = new GridUpdater(rulesGrid); gu.UpdateRules(); }
/// <summary> /// Adds rule in database /// </summary> /// <param name="rule">rule object</param> public static void AddRule(RuleModel rule) { using (SQLiteConnection connection = new SQLiteConnection(LoadConnectionString())) { connection.Open(); using (SQLiteTransaction transaction = connection.BeginTransaction()) { try { SQLiteCommand cmd = new SQLiteCommand { CommandText = "insert into rulesTable (extension, includes, isCaseSensetive, path) values (@ruleExtension, @ruleIncludes, @isCaseSensetive, @rulePath)", Connection = connection }; cmd.Parameters.AddWithValue("@ruleExtension", rule.ruleExtension); cmd.Parameters.AddWithValue("@ruleIncludes", rule.ruleIncludes); cmd.Parameters.AddWithValue("@isCaseSensetive", rule.ruleIsCaseSensetive); cmd.Parameters.AddWithValue("@rulePath", rule.rulePath); cmd.ExecuteNonQuery(); transaction.Commit(); } catch (Exception e) { MessageBox.Show(e.Message, resourceManager.GetString("dbInteractionError"), MessageBoxButtons.OK, MessageBoxIcon.Error); try { transaction.Rollback(); } catch (Exception exRollBack) { MessageBox.Show(exRollBack.Message, resourceManager.GetString("dbRollBackError"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } }