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();
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,個人建議,不喜勿噴!!