Firebird未知令牌在哪里

时间:2016-06-28 08:08:39

标签: c# sql firebird

我正在使用UPDATE语句执行INSERTWHERE命令,我发现此错误:

enter image description here

这是我的代码:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
  try
  {
     FbConnection con = new FbConnection(@"User = SYSDBA; Password = masterkey; Database = D:\TDWORK.fdb; DataSource = localhost; Port = 3050; Dialect = 3; Charset = NONE; Role = admin; Connection lifetime = 15; Pooling = true; MinPoolSize = 0; MaxPoolSize = 50; Packet Size = 8192; ServerType = 0; ");
     FbCommand cmd = new FbCommand("UPDATE OR INSERT INTO ZAPOSLENI (ULOGA) VALUES (" + dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() + ") WHERE ZAPID = " + dataGridView1.Rows[e.RowIndex].Cells[0].Value + " ", con);
     con.Open();
     cmd.ExecuteNonQuery();
     con.Close();
  }
  catch (Exception ex)
  {
     MessageBox.Show(ex.ToString());
  }
}

以下是调试器在其中插入值时命令的样子:

UPDATE OR INSERT INTO ZAPOSLENI (ULOGA) VALUES (1) WHERE ZAPID = 0 

1 个答案:

答案 0 :(得分:2)

您不能在插入或更新子句中使用WHERE(请参阅UPDATE OR INSERT)。如果要插入行,当没有ZAPID = 0的记录时,请使用以下语句:

UPDATE OR INSERT INTO ZAPOSLENI (ZAPID, ULOGA) VALUES (0, 1) MATCHING (ZAPID)

编辑:完整示例

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        FbConnection con = new FbConnection(@"User = SYSDBA; Password = masterkey; Database = D:\TDWORK.fdb; DataSource = localhost; Port = 3050; Dialect = 3; Charset = NONE; Role = admin; Connection lifetime = 15; Pooling = true; MinPoolSize = 0; MaxPoolSize = 50; Packet Size = 8192; ServerType = 0; ");
        FbCommand cmd = new FbCommand("UPDATE OR INSERT INTO ZAPOSLENI (ZAPID, ULOGA) VALUES (" + dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() + ", " + dataGridView1.Rows[e.RowIndex].Cells[0].Value + ") MATCHING (ZAPID)", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}