我将如何:
目前,用户无法确认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();
}
}
}
}
答案 0 :(得分:1)
如果我理解正确......
你想要:
Form2
Form1
Form2
)Form2
DataGridView
Form1
醇>
这是一种可能的解决方案:首先,您必须设置Form2 open方法,将其视为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()
函数并更新数据!
可能有错误,但一般的想法是......祝你好运