Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# and MySQL

C# and MySQL

Scheduled Pinned Locked Moved C#
questionhelpcsharpdatabasemysql
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Evgeni57
    wrote on last edited by
    #1

    Hi all. I have some issue with data encoding. I have a txt file, which was open like this: StreamWriter m_ComissionShopWriter = new StreamWriter(Directory.GetCurrentDirectory() + "\\logs\\1.txt", false, Encoding.GetEncoding(1251)); then I user WriteLine method to put there some data, which was taken from some stream: sr = new StreamReader(stream, Encoding.GetEncoding(1251)); After, I try to load it into table, which has the following collumn: varchar(100) cp1251_general_ci Insertion goes great! But when I run select query on it, I got some junk data instead of readable, 1251 encoded, data. What is wrong, and how can I fix it? Thanks!

    L 1 Reply Last reply
    0
    • E Evgeni57

      Hi all. I have some issue with data encoding. I have a txt file, which was open like this: StreamWriter m_ComissionShopWriter = new StreamWriter(Directory.GetCurrentDirectory() + "\\logs\\1.txt", false, Encoding.GetEncoding(1251)); then I user WriteLine method to put there some data, which was taken from some stream: sr = new StreamReader(stream, Encoding.GetEncoding(1251)); After, I try to load it into table, which has the following collumn: varchar(100) cp1251_general_ci Insertion goes great! But when I run select query on it, I got some junk data instead of readable, 1251 encoded, data. What is wrong, and how can I fix it? Thanks!

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      When you open a stream with a specific encoding, it applies to the stream, not the data in memory; in .NET strings in memory are using Unicode no matter what. So you are reading/writing Unicode strings from/to the database, which requires nvarchar fields. If you insist on having a particular encoding in the database, then you must read/write byte arrays, which you convert to/from Unicode strings using Encoding.GetString()/GetBytes(). :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


      E 1 Reply Last reply
      0
      • L Luc Pattyn

        When you open a stream with a specific encoding, it applies to the stream, not the data in memory; in .NET strings in memory are using Unicode no matter what. So you are reading/writing Unicode strings from/to the database, which requires nvarchar fields. If you insist on having a particular encoding in the database, then you must read/write byte arrays, which you convert to/from Unicode strings using Encoding.GetString()/GetBytes(). :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


        E Offline
        E Offline
        Evgeni57
        wrote on last edited by
        #3

        Well, I tried it but it didn't work. Here is a part of my code, can you tell me, please, what I did wrong?

                MySqlConnection connection = new MySqlConnection(MyConString);
                MySqlCommand command = connection.CreateCommand();
                try
                {
                    connection.Open();
                    Console.WriteLine("Connected to DB");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Failed to connect to DB");
                    return;
                }
                command.CommandText = "truncate table test\_tb";
                command.ExecuteNonQuery();
                connection.Close();
        
        
                StreamReader sr = new StreamReader("somefile.txt", Encoding.GetEncoding(1251));
                string line, query;
                string\[\] data;
                byte\[\] temp = new byte\[10000\];
                int j = 0;
                while ((line = sr.ReadLine()) != null && j < 10)
                {
                    data = line.Split('~');
        
                    query = "insert into test\_tb (name, id) values(N'" + data\[2\] + "'," + j.ToString() + ")";
                    /\*Some parsing was done here, data\[2\] contains 1251 encoded string\*/
                    temp = ASCIIEncoding.GetEncoding(1251).GetBytes(query);
                    command.CommandText = ASCIIEncoding.GetEncoding(1251).GetString(temp);
                    connection.Open();
                    command.ExecuteNonQuery();
                    connection.Close();
                    ++j;
                }
        

        The column in table os varchar(1000) type, with windows-1251 encoding thanks a lot.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups