【问题标题】:Displaying messagebox after MDB updateMDB 更新后显示消息框
【发布时间】:2013-12-29 19:20:53
【问题描述】:

我会怎么做:

  1. 在成功输入 MDB SQL 后创建一个确认对话框(只有 ok 选项) - 表单将关闭
  2. 在 form1 上更新 datagridview1 以反映新的更改,此更新只能在用户单击确认对话框上的确定按钮或在 MDB SQL 输入之后发生。

目前,用户无法确认 SQL 条目是否成功。 Form2 通过 Form1 打开,Form1 在 Form2 打开时保持打开状态 - 因此需要在后台进行更新。无需重新打开 Form1。

如果您需要任何进一步的信息,请告诉我。

这是启动一切的代码:

private void save_btn_Click(object sender, EventArgs e)
{

    if (pgpText.Text.Trim().Length == 0)
    {
        MessageBox.Show("Please fill the following textbox: PGP");
    }
    else if (teamText.Text.Trim().Length == 0)
    {
        MessageBox.Show("Please fill the following textbox: Team");
    }
    else
    {

        using (OleDbConnection conn = new OleDbConnection())
        {
            string pgp_new = pgpText.Text;
            string pgp_old = pgpOld.Text;
            string team = teamText.Text;
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='db.mdb'";
            OleDbCommand command = new OleDbCommand();
            command.Connection = conn;
            command.CommandText = "UPDATE PGP SET PGP=?,Team=? WHERE PGP=?";
            command.Parameters.Add("pgp_new", OleDbType.VarChar).Value = pgp_new;
            command.Parameters.Add("team", OleDbType.VarChar).Value = team;
            command.Parameters.Add("pgp_old", OleDbType.VarChar).Value = pgp_old;
            conn.Open();

            int affectedRows = (int)command.ExecuteNonQuery();

            if (affectedRows == 0)
            {
                command.CommandText = "INSERT INTO PGP (PGP,Team) VALUES (?, ?)";
                command.Parameters.RemoveAt(2);
                command.ExecuteNonQuery();
            }
        }
    }
}

【问题讨论】:

    标签: c# sql winforms ms-access datagridview


    【解决方案1】:

    如果我理解正确的话……

    你想:

    1. Form1打开Form2
    2. 执行您的 SQL(在 Form2 中)
    3. 关闭Form2
    4. Form1 中更新DataGridView

    这是一个可能的解决方案:首先您必须设置您的 Form2 打开方法,将其视为Dialog。你在Form1写这样的东西

    private void openForm2()
    {
        var f2 = new Form2(); //create new form2
        var formResult = f2.ShowDialog(); //open as Dialog and check result after close event
        if (formResult == DialogResult.OK) //check form2 dialog result
        {
            //form2 gave OK, I can update my DGV and display msg
            MessageBox.Show("DGV will be updated", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //update my DGV
            UpdateDGV();
        }
        else
        {
            //form2 passed Cancel or something else, not good
            MessageBox.Show("Form2 Closed but nothing happened.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }   
    

    现在您已经打开了 Form2,您必须检查您的查询是否执行了某些操作...如果成功,您可以传递 OK 结果并关闭它。这就是 Form2 中发生的情况

    int affectedRows = (int)command.ExecuteNonQuery();
    
    if (affectedRows == 0)
    {
        //no records to UPDATE, will INSERT
        command.CommandText = "INSERT INTO PGP (PGP,Team) VALUES (?, ?)";
        command.Parameters.RemoveAt(2);
        //rows affected by INSERT statement
        affectedRows = (int)command.ExecuteNonQuery();
    }
    
    if (affectedRows > 0)
    {
         //ok some rows were affected, close the form and pass OK result
         this.DialogResult = DialogResult.OK;
    }
    

    最后,在 Form1 中编写您的 UpdateDGV() 函数并更新您的数据!!!

    可能会有错误,但总体思路是这样的……祝你好运

    【讨论】:

    • 您应该更新您的答案,表明如果没有记录,affectedRows 应该由插入语句重新初始化
    • 我正在尝试更新表单 1 上的 datagridview1
    猜你喜欢
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    相关资源
    最近更新 更多