Description:
Using DataAdapter.FillSchema to add an empty DataTable to a DataSet that
contains the MySQL database schema. All the columns and their types and whether
or not they allow nulls are set correctly, but the default values are not set
correctly. As a result, when creating a new row, values are initialized to
DBNull instead of the correct default values.
This is the same bug that was reported for 1.0.4. After reporting that bug, I noticed there was a newer version available so I downloaded and installed it. The new version has the same problem.
How to repeat:
Create a table with default values for one or more columns. Then in the
program, create an empty table with database schema programmatically:
*------------------*
DataSet dataSet = new DataSet();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter("select * from <tablename>
limit 1", (MySqlConnection)DB.connection);
MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(dataAdapter);
dataAdapter.FillSchema(dataSet, System.Data.SchemaType.Source);
commandBuilder.RefreshSchema();
*------------------*
dataSet.Tables["Table"].Columns will contain the collection of columns and will
know the correct column names and types from the call to FillSchema. But the
DataColumn.DefaultValue property will not be set for any of the columns. This
can be evidenced by calling dataSet.Tables["Table"].NewRow(). The resulting
DataRow object will have all fields initialized to DBNull instead of the default
values from the database schema.
Right now the only way to work around this is to hard-code the default values
into my program. Obviously it would be better to retrieve the default values
from the schema in case they change later.
Suggested fix:
Update the MySqlDataAdapter.FillSchema method to populate the default values in
the resulting DataTable.Columns' DefaultValue properties. Then the .NET
framework should be able to handle the rest.