Connecting to MySQL with ODBC
-
Hi guys, I think it's a simple question: I connect to a MySQL database with
OdbcDataAdapterand besides the data 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
MaxLengthproperty of theDataColumnmember 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' theMaxLengthproperty is 4. But I specified the 'name' column to allow 100 chars while creating the table (... name VARCHAR(100) NOT NULL, ...). Furthermore I'm not able to change theMaxLengthproperty. 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 P.S.: Sorry I also posted this in SQL/ADO/ADO.NET forum but perhaps it fits better here...
-
Hi guys, I think it's a simple question: I connect to a MySQL database with
OdbcDataAdapterand besides the data 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
MaxLengthproperty of theDataColumnmember 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' theMaxLengthproperty is 4. But I specified the 'name' column to allow 100 chars while creating the table (... name VARCHAR(100) NOT NULL, ...). Furthermore I'm not able to change theMaxLengthproperty. 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 P.S.: Sorry I also posted this in SQL/ADO/ADO.NET forum but perhaps it fits better here...
The schema that you get is relative to the result set. If you want to have a specific schema that matches your tables, I suggest you create a typed
DataSetby using either the xsd.exe utility that ships with the .NET Framework SDK to create a typedDataSetclass from an XML Schema, or add a newDataSetclass in Visual Studio .NET and either re-create your tables (and optionally your keys and relationships), or drag and drop your tables from the Server Explorer after adding a connection object to your database. Use that typedDataSetin yourOdbcDataAdaptercalls (likeFill) and in your queries. If you're using the designer in VS.NET for aDataGridor some other data-bound control, it gives you nice design-time support for specific table names and columns. If also provides faster access to data when using the typed table and column names because they reference theDataTableandDataColumnobjects directly, instead of using a string name that requires several looks ups. If you don't want to use a typedDataSet, you can also add a columns to yourTableMappingproperty. See theDataTableMappingclass and itsColumnMappingsproperty documentation in the .NET Framework SDK for more information.Microsoft MVP, Visual C# My Articles
-
The schema that you get is relative to the result set. If you want to have a specific schema that matches your tables, I suggest you create a typed
DataSetby using either the xsd.exe utility that ships with the .NET Framework SDK to create a typedDataSetclass from an XML Schema, or add a newDataSetclass in Visual Studio .NET and either re-create your tables (and optionally your keys and relationships), or drag and drop your tables from the Server Explorer after adding a connection object to your database. Use that typedDataSetin yourOdbcDataAdaptercalls (likeFill) and in your queries. If you're using the designer in VS.NET for aDataGridor some other data-bound control, it gives you nice design-time support for specific table names and columns. If also provides faster access to data when using the typed table and column names because they reference theDataTableandDataColumnobjects directly, instead of using a string name that requires several looks ups. If you don't want to use a typedDataSet, you can also add a columns to yourTableMappingproperty. See theDataTableMappingclass and itsColumnMappingsproperty documentation in the .NET Framework SDK for more information.Microsoft MVP, Visual C# My Articles
I got it working with a typed
DataSet... thanks very much for your help Heath!!! You're the best! :rose: Regards, mYkel