VS2005連接MySQL數(shù)據(jù)庫(kù)的實(shí)際操作流程
以下的文章主要是向大家講述的是用VS2005來(lái)連接MySQL數(shù)據(jù)庫(kù)的實(shí)際操作步驟,最近做了下關(guān)于MySQL的數(shù)據(jù)庫(kù)操作,但是在以前并沒(méi)有實(shí)踐,把我的方法寫(xiě)一下,MySQL數(shù)據(jù)庫(kù)新手可以借鑒下。
我這里是用的connector/net連接MySQL的。
1.去http://dev.MySQL.com/downloads/connector/net/5.0.html下載MySQL Connector/Net 5.0,我用的是5.0.8.1,下載后安裝
2.到安裝目錄,如D:\Program Files\MySQL\MySQL Connector Net 5.0.8.1\Binaries\.NET 2.0拷貝MySQL.Data.dll到你的項(xiàng)目bin下,或者直接引用它。
3.添加using MySQL.Data.MySQLClient;
4.下面是我的程序:
- string connStr = String.Format("server={0};user id={1}; password={2}; database={3}; pooling=false;port=3306", "127.0.0.1", "test", "test", "test");
- MySQLConnection myConn = new MySQLConnection(connStr);
- myConn.Open();
- MySQLCommand cmd = myConn.CreateCommand();
- string sql1 = "CREATE TEMPORARY TABLE tmp_table (maxonline int,srv_ip VARCHAR(30),fld_date datetime)";
- string sql2 = @"insert into tmp_table SELECT 33,'ip1','2007-5-5'";
測(cè)試數(shù)據(jù)
- string sql3 = "select sum(maxonline),DATE_FORMAT(fld_date, '%Y-%m-%d') from tmp_table group by fld_date";
- cmd.CommandText = sql1;
- cmd.ExecuteNonQuery();
- cmd.CommandText = sql2;
- cmd.ExecuteNonQuery();
- cmd.CommandText = sql3;
- MySQLDataReader rd = cmd.ExecuteReader();
- GridView1.DataSource = rd;
- GridView1.DataBind();
- myConn.Close();
因?yàn)檫@個(gè)MySQL數(shù)據(jù)庫(kù)用的是比較老的版本,不支持select的嵌套,所以用了臨時(shí)表。以上的相關(guān)內(nèi)容就是對(duì)用VS2005連接MySQL數(shù)據(jù)庫(kù)的介紹,望你能有所收獲。
【編輯推薦】
- 解決MySQL無(wú)法遠(yuǎn)程訪問(wèn)的3方案
- 甲骨文對(duì)MySQL數(shù)據(jù)庫(kù)做出十大承諾!
- MySQL 常用語(yǔ)句的基本用法的介紹
- 安裝MySQL-python的實(shí)際操作步驟
- 對(duì)MySQL數(shù)據(jù)庫(kù)中Table is read only的殲滅