标签:
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(); }
标签:
原文地址:http://www.cnblogs.com/wordgao/p/4505759.html