从DataGrid中选择行,然后按Delete按钮ERROR:42601

时间:2017-11-15 07:04:12

标签: c# wpf postgresql

private void Delete_Click(object sender, RoutedEventArgs e)
        {
            if (dataGrid1.SelectedItem == null)
                return;
            DataRowView rowview = (DataRowView)dataGrid1.SelectedItem;
            NpgsqlConnection con = new NpgsqlConnection("Server=10.0.5.22;Port=5432;Database=TEST_DB;User Id=postgres;Password=test;");
            con.Open();
            NpgsqlCommand cmd = new NpgsqlCommand("delete from Product where productcode="+rowview["productcode"]+"'", con);
            cmd.ExecuteNonQuery();  // here it is showing error as ERROR: 42601: syntax error at or near ":"
            con.Close();
        }

当我从 DataGrid 中选择行并按Delete按钮时,会显示ERROR:

  

42601:“或”附近的语法错误(接近cmd.ExecuteNonQuery

1 个答案:

答案 0 :(得分:3)

我认为你错过了 productcode =''" 附近的撇号(') 请试试这个。

private void Delete_Click(object sender, RoutedEventArgs e)
    {
        if (dataGrid1.SelectedItem == null)
            return;
        DataRowView rowview = (DataRowView)dataGrid1.SelectedItem;
        NpgsqlConnection con = new NpgsqlConnection("Server=10.0.5.22;Port=5432;Database=TEST_DB;User Id=postgres;Password=test;");
        con.Open();
        NpgsqlCommand cmd = new NpgsqlCommand("delete from Product where productcode='" + rowview["productcode"] + "'", con);
        cmd.ExecuteNonQuery();  // here it is showing error as ERROR: 42601: syntax error at or near ":"
        con.Close();
    }