简体   繁体   中英

How to avoid invalid UTF8 byte sequence in parameterized SQL queries?

I've got a PostgreSQL database that uses the UTF8 encoding. I believe this is the default encoding.

I've got a GUI application that issues a typical SELECT query, with a WHERE clause. The query is parameterized, and the content of the parameter is usually pasted in by the end user. The value is not read from a text file, etc - it's pasted into a memo edit.

Here is the query ...

cmd.CommandText = @"SELECT history_id, emplid, displayname
                    FROM ""MAX_HISTORY_IDS""
                    WHERE searchname ILIKE :searchname";

cmd.Parameters.Add("searchname", PgSqlType.VarChar).Value = "Sievert,Jürgen";

In the above code, the parameter value, Sievert,Jürgen , would be a variable parsed out of a memo edit box, or text input, etc. That name is also a real example of a name causing this query to result in an invalid UTF8 byte sequence error, from Postgre.

Now, I assume the UTF8 encoding, on the database, is fine and is pretty standard for this kind of content. What's wrong here? Is the encoding of this text parameter not UTF8? How can I get this query to work, or is the encoding on the database not suitable?

I figured it out. I'm using the DotConnect PostgreSQL client library. In the connection string I simply needed to add Unicode=True . This seems to make everything work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM