成功从数据库中删除数据,但它不会更新,直到关闭表单并再次运行它

时间:2014-04-11 17:24:02

标签: c# winforms ms-access

我已经可以通过表单从数据库中删除数据了,我使用AutoCompleteStringCollection从数据库中搜索StudentID,但是当它成功删除时,引用StudentID的AutoCompleteStringCollection在我关闭表单之前不会更新并再次运行它。

以下是我用于删除的代码和AutoCompleteStringCollection:

AutoCompleteStringCollection _collection = new AutoCompleteStringCollection();

private void DeleteStudent_Load(object sender, EventArgs e)
        {
            Collection(sender, e);
        }

private void Collection(object sender, EventArgs e)
        {
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                string query = "SELECT DISTINCT [StudentID] FROM [Student] ORDER BY [StudentID] ASC";

                conn.Open();

                using (OleDbCommand cmd = new OleDbCommand(query, conn))
                using (OleDbDataReader dReader = cmd.ExecuteReader())
                {
                    while (dReader.Read())
                    {
                        string _studentID = dReader["StudentID"].ToString();
                        _collection.Add(_studentID);
                    }

                    dReader.Close();
                }

                this.textBox9.AutoCompleteMode = AutoCompleteMode.Suggest;
                this.textBox9.AutoCompleteSource = AutoCompleteSource.CustomSource;
                this.textBox9.AutoCompleteCustomSource = _collection;

                conn.Close();
            }
        }

private void Deleted(object sender, EventArgs e)
        {
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                string commandUpdate = "DELETE FROM [Student] WHERE [StudentID] = @StudentID";

                conn.Open();

                using (OleDbCommand cmd = new OleDbCommand(commandUpdate, conn))
                {
                    cmd.Parameters.Add("@StudentID", System.Data.OleDb.OleDbType.VarChar);
                    cmd.Parameters["@StudentID"].Value = this.textBox1.Text;

                    cmd.ExecuteNonQuery();
                }

                _wait.ShowDialog();

                System.Media.SoundPlayer _sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                _sounds.Play();

                DialogResult _dialogResult = MessageBox.Show("Deleted Successfully!", "Deleted");

                if (_dialogResult == DialogResult.OK)
                {
                    button2_Click(sender, e);
                }

                conn.Close();
            }
        }

这是图片:

予。在下图中,您可以看到AutoCompleteStringCollection显示“TP395134”StudentID。

enter image description here

II。在下图中,您可以看到数据库存储了“TP395134”StudentID。

enter image description here

III。当我通过表单从数据库中删除“TP395134”StudentID时,数据库编号2中没有数据。但是AutoCompleteStringCollection仍然显示“TP395134”,如数字1,即使我已经删除它并且AutoCompleteStringCollection赢了' t更新,直到表格关闭。

问题:当我成功删除对应的StudentID而不必关闭表单并再次打开它时,如何更新AutoCompleteStringCollection?

您的回答非常感谢!谢谢!

2 个答案:

答案 0 :(得分:2)

从您的收藏中删除数据库的记录remove

_collection.Remove(this.textBox1.Text);

答案 1 :(得分:0)

在delete方法中执行Collection()方法......