#region 建立MySql数据库连接
///
/// 建立数据库连接.
///
///返回MySqlConnection对象
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命令
///
/// 执行MySqlCommand
///
/// SQL语句
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对象
///
/// 创建一个MySqlDataReader对象
///
/// SQL语句
///返回MySqlDataReader对象
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
mysql.data.dll下载_c#连接mysql必要插件
全部版本下载:http://hovertree.com/h/bjaf/0sft36s9.htm
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,我们就可以顺利的使用该类库的函数建立连接了。
以下是几个常用函数:
1 #region 建立MySql数据库连接 2 ///
1 using System.Data; 2 3 using MySql.Data.MySqlClient; 4 5 6 7 8 9 private MySqlConnection conn; 10 11 private DataTable data; 12 13 private MySqlDataAdapter da; 14 15 private MySqlCommandBuilder cb; 16 17 private DataGrid dataGrid; 18 19 20 21 private void connectBtn_Click(object sender, System.EventArgs e) 22 23 { 24 25 if (conn != null) 26 27 conn.Close(); 28 29 30 31 string connStr = String.Format("server={0};user id={1}; password={2}; port={3}; database=mysql; pooling=false; charset=utf8", 32 33 server.Text, userid.Text, password.Text, 3306); 34 35 36 37 try 38 39 { 40 41 conn = new MySqlConnection( connStr ); 42 43 conn.Open(); 44 45 46 47 GetDatabases(); 48 49 MessageBox.Show("连接数据库成功!"); 50 51 } 52 53 catch (MySqlException ex)
54 55 { 56 57 MessageBox.Show( "Error connecting to the server: " + ex.Message ); 58 59 } 60 61 } 62 63 64 65 private void GetDatabases()
66 67 { 68 69 MySqlDataReader reader = null; 70 71 MySqlCommand cmd = new MySqlCommand("SHOW DATABASES", conn); 72 73 74 75 try 76 77 { 78 79 reader = cmd.ExecuteReader(); 80 81 databaseList.Items.Clear(); 82 83 while (reader.Read())
84 85 { 86 87 databaseList.Items.Add( reader.GetString(0) ); 88 89 } 90 91 } 92 93 catch (MySqlException ex)
94 95 { 96 97 MessageBox.Show("Failed to populate database list: " + ex.Message ); 98 99 }100 101 finally 102 103 {104 105 if (reader != null) reader.Close();106 107 }108 109 }110 111 112 113 private void databaseList_SelectedIndexChanged(object sender, System.EventArgs e)114 115 {116 117 MySqlDataReader reader = null;118 119 conn.ChangeDatabase(databaseList.SelectedItem.ToString());120 121 //http://sosoft.cnblogs.com/122 123 MySqlCommand cmd = new MySqlCommand("SHOW TABLES", conn);124 125 try 126 127 {128 129 reader = cmd.ExecuteReader();130 131 tables.Items.Clear();132 133 while (reader.Read())
134 135 {136 137 tables.Items.Add( reader.GetString(0) );138 139 }140 141 }142 143 catch (MySqlException ex)
144 145 {146 147 MessageBox.Show("Failed to populate table list: " + ex.Message );148 149 }150 151 finally 152 153 {154 155 if (reader != null) reader.Close();156 157 }158 159 }160 161 162 163 private void tables_SelectedIndexChanged(object sender, System.EventArgs e)164 165 {166 167 data = new DataTable();168 169 170 171 da = new MySqlDataAdapter("SELECT * FROM " + tables.SelectedItem.ToString(), conn );172 173 cb = new MySqlCommandBuilder( da ); // 此处必须有,否则无法更新174 175 176 177 da.Fill( data );178 179 180 181 dataGrid.DataSource = data;182 183 }184 185 186 187 private void updateBtn_Click(object sender, System.EventArgs e)188 189 {190 191 DataTable changes = data.GetChanges();192 193 da.Update( changes );194 195 data.AcceptChanges();196 197 }