Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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