Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

like statement for npgsql using parameter

Tags:

c#

asp.net

npgsql

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 name is "LocationName". I'm using ASP.net with C#.

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();

I get this exception:

Npgsql.NpgsqlException: ERROR: 42703: column "%((E'My place'))%" does not exist

I've tried running the query without using %, but it doesn't work. I've also tried using + and & like given below but that didn't work either:

string query = "Select \"LocationName\" from \"Locations\" where \"LocationName\" LIKE '%'+ :loc_name +'%'";

with the above line, i get this exception:

Npgsql.NpgsqlException: ERROR: 42725: operator is not unique: unknown + unknown
like image 825
NamrataP Avatar asked Dec 19 '12 10:12

NamrataP


People also ask

How do I connect to Npgsql?

Connections can be instantiated directly, and must then be opened before they can be used: var connectionString = "Host=myserver;Username=mylogin;Password=mypass;Database=mydatabase"; await using var conn = new NpgsqlConnection(connectionString); await conn. OpenAsync();

What is Npgsql connection string?

To connect to a database, the application provides a connection string which specifies parameters such as the host, the username, the password, etc. Connection strings have the form keyword1=value; keyword2=value; and are case-insensitive.

What is Npgsql in PostgreSQL?

Npgsql is an open source ADO.NET Data Provider for PostgreSQL, it allows programs written in C#, Visual Basic, F# to access the PostgreSQL database server. It is implemented in 100% C# code, is free and is open source.

What is prepared statement in PostgreSQL?

A prepared statement is a server-side object that can be used to optimize performance. When the PREPARE statement is executed, the specified statement is parsed, analyzed, and rewritten. When an EXECUTE command is subsequently issued, the prepared statement is planned and executed.


1 Answers

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 || '%'";
like image 198
Tobia Zambon Avatar answered Sep 28 '22 14:09

Tobia Zambon