.net 访问MySQL数据库

用MySQLDriverCS连接MySQL数据库  

    先下载和安装MySQLDriverCS,地址:

    http://sourceforge.net/projects/mysqldrivercs/

    在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中

    注:我下载的是版本是 MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe

 

主要代码如下:

   using MySQLDriverCS;

 

    try
            {
                MySQLConnection conn = null;

                conn = new MySQLConnection(new MySQLConnectionString("localhost", "sugarcrm", "root", "").AsString);

                conn.Open();

 

                string sql = "select * from users ";

                MySQLDataAdapter mda = new MySQLDataAdapter(sql, conn);

 

                DataSet ds = new DataSet();

                mda.Fill(ds, "table1");

 

                this.dataGridView1.DataSource = ds.Tables["table1"];


                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

posted @ 2010-07-18 16:14  海乐学习  阅读(295)  评论(0编辑  收藏  举报