C#串連mysql三種方式

來源:互聯網
上載者:User

標籤:mysql   c#   

第一種方式:
使用MySQLDriverCS.dll串連
MySQLDriverCS軟體下載:http://sourceforge.net/projects/mysqldrivercs/?source=typ_redirect
安裝完之後再引用中添加引用,找到安裝目錄,找到MySQLDriverCS.dll檔案,然後添加using MySQLDriverCS.dll檔案
參考網址:http://www.cnblogs.com/genli/articles/1956537.html
C#串連mysql代碼
MySQLConnection DBConn; 
DBConn = new MySQLConnection(new MySQLConnectionString("10.99.19.121","haha", "root", "root", 3306).AsString);
//DBConn = new MySQLConnection(new MySQLConnectionString("資料來源","資料庫名", "使用者名稱", "密碼", 連接埠號碼).AsString);
try
{
DBConn.Open(); // 執行查詢語句
MessageBox.Show("資料庫已經串連了!");
string sql = "select * from tb_user";
MySQLDataAdapter mda = new MySQLDataAdapter(sql, DBConn);
DataSet ds = new DataSet();
mda.Fill(ds, "table1");
this.dataGridView1.DataSource = ds.Tables["table1"];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
DBConn.Close();


或者這麼寫:
MySQLConnectionString constr = new MySQLConnectionString("10.99.19.121", "haha", "root", "root", 3306);
MySQLConnection DBConn = new MySQLConnection(constr.AsString);
//MySQLConnection DBConn; 
//DBConn = new MySQLConnection(new MySQLConnectionString("10.99.19.121","haha", "root", "root", 3306).AsString);


try
{
DBConn.Open(); // 執行查詢語句
MessageBox.Show("資料庫已經串連了!");
string sql = "select * from tb_user";
MySQLDataAdapter mda = new MySQLDataAdapter(sql, DBConn);
DataSet ds = new DataSet();
mda.Fill(ds, "table1");
this.dataGridView1.DataSource = ds.Tables["table1"];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
DBConn.Close();






第二種方法:
使用MySql.Data.dll串連
參考網址:http://www.cnblogs.com/sosoft/p/3906136.html
使用過程
dll檔案修複方法:
1、解壓下載的檔案。
2、複製檔案“mysql.data.dll”到系統目錄下。
3、系統目錄一般為:C:\WINNT\System32 64位系統為C:\Windows\SysWOW64
4、最後點擊開始菜單-->運行-->輸入regsvr32 mysql.data.dll 後,斷行符號即可解決錯誤提示! 
在再引用中添加引用,找到C:\Windows\SysWOW64目錄,找到mysql.data.dll檔案,然後添加using MySql.Data.MySqlClient;檔案


string M_str_sqlcon = "server=10.99.19.121;user id=root;password=root;database=haha"; //根據自己的設定
MySqlConnection mycon = new MySqlConnection();
mycon.ConnectionString = M_str_sqlcon;
try
{
mycon.Open();


    MessageBox.Show("資料庫已經串連了!");
string sql = "select * from tb_user";
MySqlDataAdapter mda = new MySqlDataAdapter(sql, mycon);
DataSet ds = new DataSet();
mda.Fill(ds, "table1");
this.dataGridView1.DataSource = ds.Tables["table1"];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
mycon.Close();          






第三種方式:

通過ODBC訪問mysql資料庫

(沒有時間研究那麼多,之後會補充進來)

個人建議C#和sqlserver配合使用很好,但是和mysql不是說不好,只是不太合適,試想,你做一個項目,你還要給人家安裝一個軟體才能串連上資料庫,感覺太麻煩,不專業,當然可以自己寫一個庫,但是很麻煩,而且又不是誰都會,所以個人建議用sqlserver,個人建議,不喜勿噴!!

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

C#串連mysql三種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.