2 using System.Collections.Generic;
5 using System.Threading.Tasks;
7 using System.Text.RegularExpressions;
13 private static Encoding _eCp850 = Encoding.GetEncoding(850);
14 private static Encoding _eUnicode = Encoding.UTF8;
15 private static Encoding _eLatin1 = Encoding.GetEncoding("ISO-8859-1");
17 public static string encode_cp850(string sText)
23 bSource = _eUnicode.GetBytes(sText);
24 bTarget = Encoding.Convert(_eUnicode, _eCp850, bSource);
25 sReturn = _eLatin1.GetString(bTarget);
30 public static string decode_cp850(byte[] sTextAsBytea)
33 byte[] bSource = sTextAsBytea;
36 bTarget = Encoding.Convert(_eCp850, _eUnicode, bSource);
37 sReturn = _eUnicode.GetString(bTarget);
42 static void Main(string[] args)
45 NpgsqlConnection conn;
46 NpgsqlTransaction tran;
47 NpgsqlCommand command;
50 conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=myuser;Password=mypassword;Database=mydatabase;Timeout=600;Pooling=false;ApplicationName=enctest");
52 tran = conn.BeginTransaction();
55 NpgsqlParameter add1 = new NpgsqlParameter("add1", NpgsqlTypes.NpgsqlDbType.Text);
56 command = new NpgsqlCommand("INSERT INTO myfile (add1) VALUES (:add1);", conn, tran);
57 add1.Value = encode_cp850("Schwanthaler Straße 75a");
59 command.Parameters.Add(add1);
60 command.ExecuteNonQuery();
63 //Read the data as bytea so as not to automatically convert into UTF-16
64 command = new NpgsqlCommand("select add1::bytea from myfile order by id desc limit 2;", conn, tran);
65 NpgsqlDataReader dr = command.ExecuteReader();
68 for (int i = 0; i < dr.FieldCount; i++)
70 // We now have bytea rather than string, but we can decode it easily enough
71 byte[] bSource = (byte[])dr[i];
72 String reEncodedString = decode_cp850(bSource);
73 Console.Write("Value: {0} \n", reEncodedString);
83 String keypress = Console.ReadLine().ToUpper();