Using MySQL with C# express

1. Download and install MySQL Connector ODBC at http://dev.mysql.com/downloads/connector/odbc/5.1.html
2. Create a new Windows Application in C# express
3. Add the System.Data.Odbc namespace
4. Add a DataGridView
5. Use the following code to fill the DataGridView
OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=myDatabase; User=root;Password=;Option=3;");
cn.Open();
OdbcCommand cmd = new OdbcCommand("Select * from test",cn);
OdbcDataAdapter ad = new OdbcDataAdapter(cmd);
DataTable dt = new DataTable();
ad.Fill(dt);
dataGridView1.DataSource = dt;

*******************************************************************************
The code should look like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=myDatabase; User=root;Password=;Option=3;");
cn.Open();
OdbcCommand cmd = new OdbcCommand("Select * from test",cn);
OdbcDataAdapter ad = new OdbcDataAdapter(cmd);
DataTable dt = new DataTable();
ad.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}

Comments

2 Responses to "Using MySQL with C# express"

michel1432008 said... September 17, 2008 at 10:45 AM

Sir c michel neh eh add koh sa imong link sir haa, mao neh akung mga blog.
http://www.latestbuzz2008.blogspot.com
htt://www.michelbolo.blogspot.com
http://ourlife143.blogspot.com
http://michel1432008.blogspot.com

Anup JP said... July 9, 2009 at 10:48 AM

There aare a lot of other things that need to be done before you can connect to a mysql database. Please follow the link given below.

http://cplus.about.com/od/howtodothingsinc/ss/mysqlnet.htm