Search code examples
c#mysqlconnector-net

Set PAD_CHAR_TO_FULL_LENGTH on MySql Connector/Net


As MySQL Reference Manual states,

The length of a CHAR column is fixed to the length that you declare when you create the table. The length can be any value from 0 to 255. When CHAR values are stored, they are right-padded with spaces to the specified length. When CHAR values are retrieved, trailing spaces are removed unless the PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.

How to enable PAD_CHAR_TO_FULL_LENGTH flag in Connector/Net? (not ODBC)


Solution

  • As documented under Server SQL Modes:

    To change the SQL mode at runtime, set the global or session sql_mode system variable using a SET statement:

    SET GLOBAL sql_mode = 'modes';
    SET SESSION sql_mode = 'modes';

    Setting the GLOBAL variable requires the SUPER privilege and affects the operation of all clients that connect from that time on. Setting the SESSION variable affects only the current client. Each client can change its session sql_mode value at any time.

    Therefore you want to execute a plain old SQL command along the following lines:

    SET SESSION sql_mode = 'PAD_CHAR_TO_FULL_LENGTH';