like statement for npgsql using parameter

前端 未结 1 1127
北海茫月
北海茫月 2020-12-31 13:36

I have a postgresql DB and i want to query the table \"Locations\" to retrieve the names of all the locations that match the name that\'s entered by the user. The column nam

相关标签:
1条回答
  • 2020-12-31 13:57

    you should use

    NpgsqlCommand cmd = new NpgsqlCommand("Select * from \"Locations\" where \"LocationName\" LIKE @loc_name", con);
    cmd.Parameters.AddWithValue("@loc_name", "%" + Location_Name + "%");
    

    you were inserting too much quotes: Postgre interpretes the string between double quote as a field/table-name. Let the parameter do the escape-string job

    P.S.: To concatenate string in Postgre you should use the || operator, see here. So your last query should be

    string query = "Select \"LocationName\" from \"Locations\" where \"LocationName\" LIKE '%' || :loc_name || '%'";
    
    0 讨论(0)
提交回复
热议问题