Solved

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

Posted on 2006-06-09
4
5,060 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now