访问OleDB无法删除

时间:2013-11-08 16:40:43

标签: c# ms-access oledb sql-delete

            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();
            }
        }
    }

ChildID列是自动编号主键。

错误的SQL查询?

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

1 个答案:

答案 0 :(得分: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();
}