sql,简单查询从db中删除记录

时间:2013-06-27 09:35:41

标签: .net firebird

我想将用户输入作为参数从我的firebird db中删除某些记录。

所以我有以下代码

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!" );
                    }
                }

            }
        }

此代码产生错误

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
*

1 个答案:

答案 0 :(得分:1)

您的查询错误,"DELETE * FROM DEMOAPP WHERE CODE= @P1"无效:*声明中应该没有DELETE

将其更改为"DELETE FROM DEMOAPP WHERE CODE= @P1"可以解决问题。