时间:2021-07-01 10:21:17 帮助过:4人阅读
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
c# 连接Mysql数据库
标签: