Connecting to MySQL with ODBC
-
Hi guys, I think it's a simple question: I connect to a MySQL database with
OdbcDataAdapter
and I want to get the schema of the database directly from the database, so I use something like this:public DataSet GetData()
{
// Create DataSet
DataSet ds = new DataSet(datasetName);
OdbcDataAdapter adapter = new OdbcDataAdapter();
adapter.SelectCommand = GetSelectAllCmd(); // Generates Query: "SELECT * FROM " + tableName
// Get schema from database
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
// Rename table
adapter.TableMappings.Add("Table", tableName);
// Get data
adapter.Fill(ds);
// Do we have errors?
if(ds.HasErrors)
ds.RejectChanges();
else
ds.AcceptChanges();
// Free resources
adapter.Dispose();
return ds;
}Everything seems working fine, but the schema isn't transfered correctly. The
MaxLength
property of theDataColumn
member has the value of the longest entry in the database. For example if there is a column called 'name' and the database has an entry like 'John' theMaxLength
property is 4. But I specified the 'name' column to allow 100 signs while creating the table (... name VARCHAR(100) NOT NULL, ...). Furthermore I'm not able to change theMaxLength
property. The following has no effect:ds.Tables[tableName].Columns["name"].MaxLength = 100;
So I'm not able to insert a name to the 'name' column that is longer than 4. I get the error "The value violates the MaxLength limit of this column". Many thanks in advance!!! Regards, mYkel