?
Solved

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

Posted on 2006-06-09
4
Medium Priority
?
5,386 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 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