Solved

UTF-8 Encoding (C# & PostgreSQL/Npgsql)

Posted on 2006-06-09
4
5,244 Views
Last Modified: 2007-12-19
Hello,

I'm writing a C# desktop application (.NET 1.1). The application simply retrieves a list of files in a directory and inserts it into a table. I'm getting the Npgsql.NpgsqlException: "ERROR: 22021: invalid UTF-8 byte sequence detected near byte 0x80". The strange thing is that this only happens with some files.

The program is very simple:

public static void main ()
{
   string [] files = System.IO.Directory.GetFiles("C:\\Temp");
   for (int i=0; i<files.Length; i++)
   {
      ExecuteNonQuery(files[i]);
   }
}

public static int ExecuteNonQuery (string filename)
{
   string sql = "";
   int  rowsAffected   = 0;
   Npgsql.NpgsqlCommand   npgsqlCommand   = null;
   Npgsql.NpgsqlConnection   npgsqlConnection      = null;

   // The query formed is: INSERT INTO "Image" VALUES (DEFAULT, 'C:\Temp\10x10.jpg', 0, 0, 0)
   sql = "INSERT INTO \"Image\" VALUES (DEFAULT, '" + filename + "', 0, 0, 0)";

   npgsqlConnection   = new Npgsql.NpgsqlConnection();
   npgsqlConnection.ConnectionString   = "The relevant connection string";
   npgsqlConnection.Open();

   npgsqlCommand   = new Npgsql.NpgsqlCommand(sql, npgsqlConnection);
   rowsAffected   = npgsqlCommand.ExecuteNonQuery();

   npgsqlConnection.Close();

   return (rowsAffected);
}

The target database is UTF-8 encoded. If I use a constant string for the filename parameneter, the code works fine.

How can I forcefully convert the sql statement to UTF-8?

Thanks,

Khan
0
Comment
Question by:raheelasadkhan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 16875660
Hi raheelasadkhan,
----------

you could look at the UTF8Encoding class
http://msdn2.microsoft.com/en-us/s064f8w2.aspx

----------
bruintje
share what you know, learn what you don't
0
 

Author Comment

by:raheelasadkhan
ID: 16875856
Hi,

I've already read up on the Encoding class and it's built-in implementations.

Take this as an example:
   string [] files = System.IO.Directory.GetFiles("C:\\Temp");
   string sql = "INSERT INTO \"TableName\" VALUES ('" + files[0] + "')"

This sql statement causes the error I explained in my previous message.

I can't find a working example to convert [sql] into a valid UTF-8 format. In fact, I'm not even sure what format [sql] is in the forst place.

Thanks,

Khan
0
 
LVL 44

Accepted Solution

by:
bruintje earned 500 total points
ID: 16875934
i read something about putting "encoding=unicode" into the connection string, this way the connection to the db is setup with the encoding

does that help?
0
 

Author Comment

by:raheelasadkhan
ID: 16876136
I'll look that up and try it. Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question