]> git.8kb.co.uk Git - postgresql/encoding_woes/blob - codepage_850.cs
a5cf91d253e3cb96fd3aeb3e73e36f0da445b896
[postgresql/encoding_woes] / codepage_850.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using Npgsql;
7 using System.Text.RegularExpressions;
8
9 namespace EncTest
10 {
11     class Program
12     {
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");
16
17         public static string encode_cp850(string sText)
18         {
19             string sReturn;
20             byte[] bSource;
21             byte[] bTarget;
22
23             bSource = _eUnicode.GetBytes(sText);
24             bTarget = Encoding.Convert(_eUnicode, _eCp850, bSource);
25             sReturn = _eLatin1.GetString(bTarget);
26
27             return sReturn;
28         }
29
30         public static string decode_cp850(byte[] sTextAsBytea)
31         {
32             string sReturn;
33             byte[] bSource = sTextAsBytea;
34             byte[] bTarget;
35
36             bTarget = Encoding.Convert(_eCp850, _eUnicode, bSource);
37             sReturn = _eUnicode.GetString(bTarget);
38
39             return sReturn;
40         }
41
42         static void Main(string[] args)
43         {
44
45             NpgsqlConnection conn;
46             NpgsqlTransaction tran;
47             NpgsqlCommand command;
48             String sIn;
49
50             conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=myuser;Password=mypassword;Database=mydatabase;Timeout=600;Pooling=false;ApplicationName=enctest");
51             conn.Open();
52             tran = conn.BeginTransaction();
53
54             /////// Writing cp850
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");
58
59             command.Parameters.Add(add1);
60             command.ExecuteNonQuery();
61
62             /////// Reading cp850
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();
66             while (dr.Read())
67             {
68                 for (int i = 0; i < dr.FieldCount; i++)
69                 {
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);
74                 }
75                 Console.WriteLine();
76             }
77
78             tran.Commit();
79             conn.Close();
80
81             conn.Open();
82
83             String keypress = Console.ReadLine().ToUpper();
84
85         }        
86     }
87 }