Skip to content

MySqlBulkCopy always inserts TimeOnly type as '00:00:00' into TIME column #1146

@Dargazo

Description

@Dargazo

Software versions
MySqlConnector version: 2.1.7
Server type (MySQL, MariaDB, Aurora, etc.) and version: MariaDB 10.6.7 (also 10.6.3)
.NET version: 6

Describe the bug
I have a .NET DataTable that holds a TimeOnly Column. When I try to bulk load it into a MariaDB table, where the TimeOnly column maps to a MariaDB 'TIME' column, it is always inserted as '00:00:00'.

Exception
BulkCopyResult indicates a truncation warning, no exception is thrown

Code sample

       private static string GetConnStr() 
        {
            MySqlConnectionStringBuilder l_Builder = new MySqlConnectionStringBuilder();
            l_Builder.Database = "db_test";
            l_Builder.Server = "localhost";
            l_Builder.Port = 3306;
            l_Builder.UserID = "*******";
            l_Builder.Password = "************";
            l_Builder.AllowLoadLocalInfile = true;
            l_Builder.AllowUserVariables = true;
            return l_Builder.ToString();
        }

        static async Task Main(string[] args)
        {
            // Setup
            using (MySqlConnection l_Conn = new MySqlConnection(GetConnStr()))
            {
                string l_DropQ = "DROP TABLE IF EXISTS `tbl`";
                string l_CreateQ = @"CREATE TABLE `tbl` (
	                                `A` TIME NOT NULL
                                )
                                COLLATE='utf8_general_ci'
                                ENGINE=InnoDB";
                l_Conn.Open();

                MySqlCommand l_DropCmd = new MySqlCommand(l_DropQ, l_Conn);
                await l_DropCmd.ExecuteNonQueryAsync();

                MySqlCommand l_CreateCmd = new MySqlCommand(l_CreateQ, l_Conn);
                await l_CreateCmd.ExecuteNonQueryAsync();
            }

            DataTable l_Table = new DataTable();
            l_Table.Columns.Add("A", typeof(TimeOnly));

            TimeOnly l_TO = TimeOnly.Parse("11:05:01");
            l_Table.Rows.Add(new object[] { l_TO });

            using (MySqlConnection l_Conn = new MySqlConnection(GetConnStr()))
            {
                await l_Conn.OpenAsync();
                
                // bulk copy the data
                var bulkCopy = new MySqlBulkCopy(l_Conn);
                bulkCopy.DestinationTableName = "tbl";
                var result = await bulkCopy.WriteToServerAsync(l_Table);
            }
        }

The MySqlBulkCopyResult now holds the message: "Data truncated for column 'A' at row 1".

Expected behavior
When the code above is executed: 1 row in the 'tbl' table, containing '11:05:01'.

Additional context
Converting the TimeOnly to a TimeSpan seems to work. i.e.

l_Table.Columns.Add("A", typeof(TimeSpan));
 l_Table.Rows.Add(new object[] { l_TO.ToTimeSpan() });

Though the warning in MySqlBulkCopyResult still remains.

I have also tested this using the DateOnly struct. This always inserts '0000-00-00' in a MariaDB DATE column, regardless of the value I use in the DataTable object.
The workaround here is to convert the DateOnly struct to a DateTime struct, and it starts working.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions