DELETE FROM combobox.text数据库

时间:2014-04-07 16:48:31

标签: c# sql

我明白了:

  

FROM子句中的语法错误;

cmdbox_Model.Text正在显示表格的名称。

这是代码

我想直接从组合框中选择表名。因此,用户选择了直接从该表中删除的模型类型。

    string Product = cmdbox_Product1.Text;
    string Model = cmdbox_Model.Text;
    string MacID = txt_MAC_id.Text;

    if (conn.State == ConnectionState.Open && (cmdbox_Product1.Text == "MODEM" && cmdbox_Model.Text == Model))
    {
        OleDbCommand cmd3 = new OleDbCommand("DELETE FROM " + cmdbox_Model.Text + "WHERE MacID = @MacID", conn);

        cmd3.Parameters.AddWithValue("@MacID", MacID);

        try
        {
                DialogResult result = MessageBox.Show("Are You Sure you Want to Delete this \""+ cmdbox_Product1.Text + "\"?", "Confirm DELETE",MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (result == DialogResult.Yes)
                {
                    cmd3.ExecuteNonQuery();


                    MessageBox.Show("Successful Deleted", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                }

                else MessageBox.Show("Failed To Delete", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); }

                conn.Close();
        }
        catch (OleDbException expe)
        {
                MessageBox.Show(expe.Message);
                MessageBox.Show("Error Failed to delete", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                conn.Close();
        }

2 个答案:

答案 0 :(得分:2)

您缺少表名与WHERE之间的空格。

试试这个:

OleDbCommand cmd3 = new OleDbCommand("DELETE FROM " + cmdbox_Model.Text + " WHERE MacID=@MacID", conn);

答案 1 :(得分:0)

您在查询中包含表名的位置在哪里?

"DELETE FROM " + cmdbox_Model.Text + "WHERE MacID=@MacID"

您还没有提到表名。

DELETE 
FROM TABLE NAME
WHERE 

是你编写查询的方式。