c#通过ODBC数据源连接Mysql数据库

1.下载安装数据库 MySQL Community Server 5.5.29我用的是X86 32bit
2.下载安装ODBC连接器 http://www.mysql.com/downloads/connector/odbc/5.2.html
3.配置数据源 :管理工具--数据源(ODBC)--系统DSN-添加-安装的odbc驱动 mysql ODBC 5.2w Driver
4.测试成功既为配置好了(success)
c#重要代码如下:
using System.Data.Odbc;
 string MyConString = "DRIVER={MySQL ODBC 5.2w Driver};" + 
                                "SERVER=localhost;" +

                                \
                                "DATABASE=mysql;" +
                                "UID=root;" +
                                "PASSWORD=password;" +
                                "OPTION=3";
            //使用Connector/ODBC连接到MySQL
            OdbcConnection MyConnection = new OdbcConnection(MyConString);
            //打开连接
            MyConnection.Open();
            OdbcCommand oCmd = new OdbcCommand("select * from user", MyConnection);
            OdbcDataAdapter oDa = new OdbcDataAdapter(oCmd);
            DataSet oDs = new DataSet();
            oDa.Fill(oDs, "username");
           // MessageBox.Show(oDs.Tables[0].Rows[0].ItemArray.Length.ToString());
            dataGridView1.DataSource = oDs.Tables["username"];
            
            //MessageBox.Show("\n !!! success, connected successfully !!!\n");
            MyConnection.Close();


你可能感兴趣的:(c#,C,Sharp)