从数据库行删除特定数据(C#)

时间:2015-03-31 21:27:10

标签: c# sql-server database winforms

我试图允许(用户)从数据库中删除数据并用更新的信息替换它。

我遇到的问题是:

  

SqlException未处理:','附近的语法不正确。

此错误出现在第一个:sqlcmd.ExecuteNonQuery()

以下是"应用编辑"的完整代码。按钮:

private void applybtn_Click(object sender, EventArgs e)
{
        Connection();
        sqlconnection.Open();

        sqlcmd = new SqlCommand("DELETE Item, Quantity, description, datasheet FROM inventory_table WHERE id= " + ID, sqlconnection);
        sqlcmd.ExecuteNonQuery();

        sqlcmd = new SqlCommand("UPDATE inventory_table SET Item = @Item, Quantity = @Quantity, description = @description, datasheet = @datasheet "+
            "WHERE id= " + ID, sqlconnection);
        sqlcmd.Parameters.AddWithValue("@Item", this.txtbx1.Text);
        sqlcmd.Parameters.AddWithValue("@Quantity", this.txtbx2.Text);
        sqlcmd.Parameters.AddWithValue("@description", this.txtbx3.Text);
        sqlcmd.Parameters.AddWithValue("@datasheet", this.txtbx4.Text);
        sqlcmd.ExecuteNonQuery();

        sqlconnection.Close();    
    }

1 个答案:

答案 0 :(得分:0)

试试这个

private void applybtn_Click(object sender, EventArgs e)
{
        Connection();
        sqlconnection.Open();

        sqlcmd = new SqlCommand("DELETE FROM inventory_table WHERE id= " + ID, sqlconnection);
        sqlcmd.ExecuteNonQuery();

        sqlcmd = new SqlCommand("UPDATE inventory_table SET Item = @Item, Quantity = @Quantity, description = @description, datasheet = @datasheet "+
            "WHERE id= " + ID, sqlconnection);
        sqlcmd.Parameters.AddWithValue("@Item", this.txtbx1.Text);
        sqlcmd.Parameters.AddWithValue("@Quantity", this.txtbx2.Text);
        sqlcmd.Parameters.AddWithValue("@description", this.txtbx3.Text);
        sqlcmd.Parameters.AddWithValue("@datasheet", this.txtbx4.Text);
        sqlcmd.ExecuteNonQuery();

        sqlconnection.Close();    
    }