-1

这是我运行程序时的datagridview(照片):

在此处输入图像描述

这是我在程序中的 GUI 的照片:

在此处输入图像描述

我希望当用户选择“ID 9”并单击“删除”按钮时,它将删除选定的行并在数据库中将其删除。

我怎么能这样做?我的意思是,我应该在我的命令中输入什么?我知道我的命令缺少“WHERE”子句,但我不知道在“WHERE”子句中放什么。

这是我现在一直在做的代码(以下代码是单击删除所有行)

deleteButton.Click += new System.EventHandler(this.DeleteRecord);

private DeleteRecord(object sender, EventArgs e)
{
    using (OleDbConnection conn = new OleDbConnection(connectionString))
                    {
                        string query = "DELETE FROM [Record]";
                        conn.Open();

                        using (OleDbCommand cmd = new OleDbCommand(query, conn))
                        {
                            using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
                            {
                                DataTable ds = new DataTable();
                                adapter.Update(ds);
                                dataGridView.DataSource = ds;
                                cmd.ExecuteNonQuery();
                            }
                        }
                    }
}

现在,我的问题是,当用户选择单行时,如何将其从 datagridview 中删除并将其更新到数据库中?请帮忙。谢谢。

编辑我已经尝试过这段代码:

    deleteButton.Click += new System.EventHandler(this.DeleteRecord);

 private void DeleteRecord(object sender, EventArgs e)
        {
            int i = dataGridView.SelectedCells[0].RowIndex;
            string strID = dataGridView.Rows[i].Cells[0].Value.ToString();

            if (fifthForm.comboBox1.Text == "English")
            {
                using (OleDbConnection conn = new OleDbConnection(connectionString))
                {
                    string query = "DELETE FROM [Record] WHERE [ID] = @ID";
                    conn.Open();

                    using (OleDbCommand cmd = new OleDbCommand(query, conn))
                    {
                        cmd.Parameters.Add("ID", System.Data.OleDb.OleDbType.Integer);

                        using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
                        {
                            DataTable ds = new DataTable();
                            cmd.Parameters["ID"].Value = strID;
                            adapter.Update(ds);
                            dataGridView.DataSource = ds;
                            cmd.ExecuteNonQuery();
                        }
                    }

                    conn.Close();
                }
            }
       }

但是为什么当我单击“删除”按钮时,它会删除datagridview中的所有记录,但它不会更新到数据库?而且我还希望只有一个已选择的选定行将被删除。

谢谢

4

2 回答 2

1

You can do it like this example code:

string query = "DELETE FROM [Record] WHERE ID = 9";

Or from some control for example:

string query = "DELETE FROM [Record] WHERE ID =" + idTextBox.Text;

Update for SelectedRow:

deleteButton.Click += new System.EventHandler(this.DeleteRecord);

private DeleteRecord(object sender, EventArgs e)
{

foreach(DataGridViewRow row in DataGridView1.SelectedRows)
{
    int rowIdToDelete = row.Cells[ID].Value;
    using (OleDbConnection conn = new OleDbConnection(connectionString))
                    {
                        string query = "DELETE FROM [Record] WHERE ID = " + rowIdToDelete;
                        conn.Open();

                        using (OleDbCommand cmd = new OleDbCommand(query, conn))
                        {
                            using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
                            {
                                DataTable ds = new DataTable();
                                adapter.Update(ds);
                                dataGridView.DataSource = ds;
                                cmd.ExecuteNonQuery();
                            }
                        }
                    }
}

}
于 2013-09-11T14:41:46.793 回答
0

You need to put a where condition in your delete statement.

Something like this:-

  using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                string query = "DELETE FROM [Record] WHERE ID =" + idTextBox.Text;
                conn.Open();

                using (OleDbCommand cmd = new OleDbCommand(query, conn))
                {
                    using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
                    {
                        DataTable ds = new DataTable();
                        adapter.Update(ds);
                        dataGridView.DataSource = ds;
                        cmd.ExecuteNonQuery();
                    }
                }
            }
于 2013-09-11T14:41:56.733 回答