?
Solved

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

Posted on 2006-06-09
4
Medium Priority
?
5,305 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

752 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