简体   繁体   中英

sql, simple query to delete record from db

I want to take users input as parameter to delete certain record from my firebird db.

So I have following code

private void btnOk_Click(object sender, EventArgs e)
        {
            string code = txtDeleteField.Text;
            string connString = ConfigurationManager.AppSettings["DemoAppConnectionString"];

            using (FbConnection conn = new FbConnection(connString))
            {
                using (FbCommand cmd = new FbCommand())
                {
                    cmd.CommandText = "DELETE * FROM DEMOAPP WHERE CODE= @P1";
                    cmd.Parameters.AddWithValue("@P1", code);
                    cmd.Connection = conn;
                    conn.Open();
                    cmd.Transaction = conn.BeginTransaction();

                    int a = cmd.ExecuteNonQuery();
                    if (a == 0)
                    {
                        MessageBox.Show("Error!");
                    }
                    else
                    {
                        MessageBox.Show("Record deleted, ok!" );
                    }
                }

            }
        }

This code produces error

An unhandled exception of type 'FirebirdSql.Data.FirebirdClient.FbException' occurred in FirebirdSql.Data.FirebirdClient.dll

Additional information: Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, column 8
*

Your query is wrong, "DELETE * FROM DEMOAPP WHERE CODE= @P1" is invalid: there should be no * in a DELETE statement.

Changing it to "DELETE FROM DEMOAPP WHERE CODE= @P1" should fix the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM