dotConnect for MySQL Documentation
Devart.Data.MySql Namespace / MySqlConnection Class / Unicode Property
Example

On this page
    Unicode Property (MySqlConnection)
    On this page
    Gets or sets a value indicating whether the UTF8 charset will be used.
    Syntax
    'Declaration
     
    Public Property Unicode As Boolean
    public bool Unicode {get; set;}

    Property Value

    true, if client charset UTF8 is used; false, if default client charset is used. The default value is false.
    Remarks
    Sets MySQL client charset to UTF8 and converts client data according to this charset.

    When a value is assigned to this property, the MySqlConnection is closed.

    MySQL server version 4.1 or higher is required to use this option.
    Example
    The following two functions demonstrate how to use Unicode property to implement charset-safe string transfer.
    public void UploadString(MySqlConnection mySqlConnection)
    {
      mySqlConnection.Unicode = true;
      MySqlCommand mySqlCommand = new MySqlCommand("INSERT INTO Test.TextBlocks (BlockID, BlockName, BlockContent) VALUES(1,'First',:BlockText)", mySqlConnection);
      mySqlCommand.Parameters.Add("BlockText",MySqlType.VarChar,50).Value = "Place here some text that requires Unicode support.";
      mySqlConnection.Open();
      try
      {
        Console.WriteLine(mySqlCommand.ExecuteNonQuery()+" rows affected.");
      }
      finally
      {
        mySqlConnection.Close();
      }
    }                                                                                                                                           
    
    public void DownloadString(MySqlConnection mySqlConnection)
    {
      mySqlConnection.Unicode = true;
      MySqlCommand mySqlCommand = new MySqlCommand("SELECT * FROM Test.TextBlocks", mySqlConnection);
      mySqlConnection.Open();
      MySqlDataReader mySqlReader = mySqlCommand.ExecuteReader(CommandBehavior.Default);
      try
      {
        while (mySqlReader.Read())
        {
          string myString = (string)mySqlReader["BlockContent"];
          Console.WriteLine(myString);
        }
      }
      finally
      {
        mySqlReader.Close();
        mySqlConnection.Close();
      }
    }
    Public Sub UploadString(ByVal mySqlConnection As MySqlConnection)
      mySqlConnection.Unicode = True
      Dim mySqlCommand As New MySqlCommand("INSERT INTO Test.TextBlocks (BlockID, BlockName, BlockContent) VALUES(1,'First',:BlockText)", mySqlConnection)
      mySqlCommand.Parameters.Add("BlockText", MySqlType.VarChar, 50).Value = "Place here some text that requires Unicode support."
      mySqlConnection.Open()
      Try
        Console.WriteLine(String.Concat(mySqlCommand.ExecuteNonQuery(), " rows affected."))
      Finally
        mySqlConnection.Close()
      End Try
    End Sub
    
    Public Sub DownloadString(ByVal mySqlConnection As MySqlConnection)
    
      mySqlConnection.Unicode = True
      Dim mySqlCommand As New MySqlCommand("SELECT * FROM Test.TextBlocks", mySqlConnection)
      mySqlConnection.Open()
      Dim mySqlReader As MySqlDataReader = mySqlCommand.ExecuteReader(CommandBehavior.Default)
      Try
        While mySqlReader.Read()
          Dim myString As String = CType(mySqlReader("BlockContent"), String)
          Console.WriteLine(myString)
        End While
      Finally
        mySqlReader.Close()
        mySqlConnection.Close()
      End Try
    End Sub
    See Also