1
            string myQuery = "DELETE FROM Child WHERE ChildID = " + int.Parse(DropDownList1.SelectedValue) + "";
            OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);

            try
            {
                myConnection.Open();
                myCommand.ExecuteNonQuery();
                Label2.Text = "Done! " + DropDownList1.SelectedValue + " Successful remove";
            }
            catch (Exception ex)
            {
                Label2.Text = "Exception in DBHandler" + ex;
            }
            finally
            {
                myConnection.Close();
            }
        }
    }

ChildIDcolumn 是一个自动编号的主键。

错误的sql查询?

从数据库中删除选定的下拉列表

4

1 回答 1

1

您在示例中缺少一些代码,但请尝试以下内容。

string query = "DELETE FROM Child WHERE ChildID = @id";
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(query, myConnection);
cmd.Parameters.AddWithValue("@id", int.Parse(DropDownList1.SelectedValue));
try
{
    myConnection.Open();
    cmd.ExecuteNonQuery();
}
finally
{
    if (myConnection.State != System.Data.ConnectionState.Closed) myConnection.Close();
}
于 2013-11-08T17:02:44.730 回答