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
答案 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();
}