mysql.data.dll下載_c#連接mysql必要插件
mysql.data.dll是C#操作MYSQL的驅動文件,是c#連接mysql必要插件,使c#語言更簡潔的操作mysql數據庫。當你的電腦彈出提示“丟失mysql.data.dll”或“無法找到mysql.data.dll”等錯誤問題,請下載本站為你提供的dll文件,使用它可以幫助用戶解決上述問題。
dll文件修復方法:
1、解壓下載的文件。
2、復制文件“mysql.data.dll”到系統目錄下。
3、系統目錄一般為:C:\WINNT\System32 64位系統為C:\Windows\SysWOW64
4、最後點擊開始菜單-->運行-->輸入regsvr32 mysql.data.dll 後,回車即可解決錯誤提示!
第三方組件:Mysql.Data.dll
說明:下載Mysql.Data.dll,然後在項目中添加該組件的引用,在代碼頁裡輸入using Mysql.Data.MysqlClient,我們就可以順利的使用該類庫的函數建立連接了。
以下是幾個常用函數:
#region 建立MySql數據庫連接 /// <summary> /// 建立數據庫連接. /// </summary> /// <returns>返回MySqlConnection對象</returns> public MySqlConnection getmysqlcon() { //http://sosoft.cnblogs.com/ string M_str_sqlcon = "server=localhost;user id=root;password=root;database=abc"; //根據自己的設置 MySqlConnection myCon = new MySqlConnection(M_str_sqlcon); return myCon; } #endregion #region 執行MySqlCommand命令 /// <summary> /// 執行MySqlCommand /// </summary> /// <param name="M_str_sqlstr">SQL語句</param> public void getmysqlcom(string M_str_sqlstr) { MySqlConnection mysqlcon = this.getmysqlcon(); mysqlcon.Open(); MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon); mysqlcom.ExecuteNonQuery(); mysqlcom.Dispose(); mysqlcon.Close(); mysqlcon.Dispose(); } #endregion #region 創建MySqlDataReader對象 /// <summary> /// 創建一個MySqlDataReader對象 /// </summary> /// <param name="M_str_sqlstr">SQL語句</param> /// <returns>返回MySqlDataReader對象</returns> public MySqlDataReader getmysqlread(string M_str_sqlstr) { MySqlConnection mysqlcon = this.getmysqlcon(); MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon); mysqlcon.Open(); MySqlDataReader mysqlread = mysqlcom.ExecuteReader(CommandBehavior.CloseConnection); return mysqlread; } #endregion
using System.Data; using MySql.Data.MySqlClient; private MySqlConnection conn; private DataTable data; private MySqlDataAdapter da; private MySqlCommandBuilder cb; private DataGrid dataGrid; private void connectBtn_Click(object sender, System.EventArgs e) { if (conn != null) conn.Close(); string connStr = String.Format("server={0};user id={1}; password={2}; port={3}; database=mysql; pooling=false; charset=utf8", server.Text, userid.Text, password.Text, 3306); try { conn = new MySqlConnection( connStr ); conn.Open(); GetDatabases(); MessageBox.Show("連接數據庫成功!"); } catch (MySqlException ex) { MessageBox.Show( "Error connecting to the server: " + ex.Message ); } } private void GetDatabases() { MySqlDataReader reader = null; MySqlCommand cmd = new MySqlCommand("SHOW DATABASES", conn); try { reader = cmd.ExecuteReader(); databaseList.Items.Clear(); while (reader.Read()) { databaseList.Items.Add( reader.GetString(0) ); } } catch (MySqlException ex) { MessageBox.Show("Failed to populate database list: " + ex.Message ); } finally { if (reader != null) reader.Close(); } } private void databaseList_SelectedIndexChanged(object sender, System.EventArgs e) { MySqlDataReader reader = null; conn.ChangeDatabase(databaseList.SelectedItem.ToString()); //http://sosoft.cnblogs.com/ MySqlCommand cmd = new MySqlCommand("SHOW TABLES", conn); try { reader = cmd.ExecuteReader(); tables.Items.Clear(); while (reader.Read()) { tables.Items.Add( reader.GetString(0) ); } } catch (MySqlException ex) { MessageBox.Show("Failed to populate table list: " + ex.Message ); } finally { if (reader != null) reader.Close(); } } private void tables_SelectedIndexChanged(object sender, System.EventArgs e) { data = new DataTable(); da = new MySqlDataAdapter("SELECT * FROM " + tables.SelectedItem.ToString(), conn ); cb = new MySqlCommandBuilder( da ); // 此處必須有,否則無法更新 da.Fill( data ); dataGrid.DataSource = data; } private void updateBtn_Click(object sender, System.EventArgs e) { DataTable changes = data.GetChanges(); da.Update( changes ); data.AcceptChanges(); }