c# 连接Mysql数据库

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,我们就可以顺利的使用该类库的函数建立连接了。

以下是几个常用函数:

复制代码
 1 #region  建立MySql数据库连接
 2     /// <summary>
 3     /// 建立数据库连接.
 4     /// </summary>
 5     /// <returns>返回MySqlConnection对象</returns>
 6     public MySqlConnection getmysqlcon()
 7     {
 8     //http://sosoft.cnblogs.com/
 9         string M_str_sqlcon = "server=localhost;user id=root;password=root;database=abc"; //根据自己的设置
10         MySqlConnection myCon = new MySqlConnection(M_str_sqlcon);
11         return myCon;
12     }
13     #endregion
14 
15     #region  执行MySqlCommand命令
16     /// <summary>
17     /// 执行MySqlCommand
18     /// </summary>
19     /// <param name="M_str_sqlstr">SQL语句</param>
20     public void getmysqlcom(string M_str_sqlstr)
21     {
22         MySqlConnection mysqlcon = this.getmysqlcon();
23         mysqlcon.Open();
24         MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon);
25         mysqlcom.ExecuteNonQuery();
26         mysqlcom.Dispose();
27         mysqlcon.Close();
28         mysqlcon.Dispose();
29     }
30     #endregion
31 
32 #region  创建MySqlDataReader对象
33     /// <summary>
34     /// 创建一个MySqlDataReader对象
35     /// </summary>
36     /// <param name="M_str_sqlstr">SQL语句</param>
37     /// <returns>返回MySqlDataReader对象</returns>
38     public MySqlDataReader getmysqlread(string M_str_sqlstr)
39     {
40         MySqlConnection mysqlcon = this.getmysqlcon();
41         MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon);
42         mysqlcon.Open();
43         MySqlDataReader mysqlread = mysqlcom.ExecuteReader(CommandBehavior.CloseConnection);
44         return mysqlread;
45     }
46     #endregion
复制代码

 

 

 

 

复制代码
  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  }
复制代码

 http://www.cnblogs.com/sosoft/p/kaifajishu.html

posted @ 2016-02-17 15:28  JaneAi  阅读(1824)  评论(0编辑  收藏  举报