lI am using the Data Application block for a majority of my data access, specifically using the SqlHelper class to call the ExecuteReader, ExecuteNonQuery, and like methods. Passing the connection string with each database call.
How can I modify this to enable connection to a MySQL database as well.
If you've got the Enterprise Library installed and already know how to connect to SQL Server databases, connecting to MySQL databases is not any harder.
One way to do it is to use ODBC. This is what I did:
public List<Contact> Contact_SelectAll() { List<Contact> contactList = new List<Contact>(); Database db = DatabaseFactory.CreateDatabase("MySqlDatabaseTest"); DbCommand dbCommand = db.GetSqlStringCommand("select * from Contact"); using (IDataReader dataReader = db.ExecuteReader(dbCommand)) { while (dataReader.Read()) { Contact contact = new Contact(); contact.ID = (int) dataReader["ContactID"]; client.FirstName = dataReader["ContactFName"].ToString(); client.LastName = dataReader["ContactLName"].ToString(); clientList.Add(client); } } return clientList; }
Another way to do it is to build and use a MySql provider. This guy did that. I learned how to do this by adapting these instructions for connecting to Access. Oh, and here are some more MySql Connection String samples.