我有一个 postgresql 数据库,我想查询表“位置”以检索与用户输入的名称匹配的所有位置的名称。列名是“LocationName”。我正在使用带有 C# 的 ASP.net。
NpgsqlConnection con = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ToString());
NpgsqlCommand cmd = new NpgsqlCommand("Select * from \"Locations\" where \"LocationName\" LIKE \"%@loc_name%\"", con);
cmd.Parameters.AddWithValue("@loc_name", Location_Name);
NpgsqlDataReader reader = cmd.ExecuteReader();
我得到这个例外:
Npgsql.NpgsqlException: ERROR: 42703: column "%((E'My place'))%" does not exist
我尝试在不使用 % 的情况下运行查询,但它不起作用。我也尝试过使用 + 和 & 如下所示,但这也不起作用:
string query = "Select \"LocationName\" from \"Locations\" where \"LocationName\" LIKE '%'+ :loc_name +'%'";
使用上面的行,我得到了这个异常:
Npgsql.NpgsqlException: ERROR: 42725: operator is not unique: unknown + unknown