1. Home
  2. Computing & Technology
  3. C / C++ / C#

How to Use MySQL Database with Visual C# 2008 Express Edition

By David Bolton, About.com

4 of 4

Using MySQL in C#

Dog List Form
Download the Example Sources.

Technically the program connects to the ODBC connection which manages the MySQL.

This is done in two places. The first in the file windata.cs is a short class that creates, initializes and returns a .NET OdbcConnection object in the Connection property.

class sql
{
    private string odbcconnstr="Driver={MySQL ODBC 3.51
Driver};Server=localhost;Database=mydogshows3; User=root;Option=3";

    private OdbcConnection connection;

    public sql()
    {
       connection = new OdbcConnection(odbcconnstr) ;
    }

    public OdbcConnection Connection
    {
        get
        {
            return connection;
        }
    }
}

Next is connecting this to a DataGridView component and this is done in the Forms InitializeDatagridView using a BindingSource object. The DataGridView is a new component in .NET 3.5 and very flexible. As I said earlier it's best used for looking at data but not modifying it.

C# has several classes for connecting with databases. In this case we use an OdbcConnection to talk to the MySQL Connector. Had we used one of the OleDB data types that you can see when you configure with the .udl file (eg Microsoft Jet for MS Access file) you would use an OleDbConnection class.

Conclusion

It is possible to use MySQL with Microsoft Visual C# 2008 Express but you have to manage you own data sources and can't use those built in (as far as I know).
Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C# / C Sharp
  5. How To Do Things in C#
  6. Using MySQL in C#

©2009 About.com, a part of The New York Times Company.

All rights reserved.