使用之前需要先在 vs中安装MySql.Data 插件,本文采用 Nuget 方式安装 ,步骤如下:
选中项目右键——》管理Nuget程序包
输入 MySql.Data ,搜索安装即可
DataTable dt = new DataTable(); MySqlDataAdapter DataAdapter = new MySqlDataAdapter(sql, conn); // 将数据填充到dataTable中 DataAdapter.Fill(dt);
DataSet ds = new DataSet(); MySqlDataAdapter DataAdapter = new MySqlDataAdapter(sql, conn); // 将数据填充到dataSet中 DataAdapter.Fill(ds);
DataSet ds = new DataSet(); MySqlDataAdapter DataAdapter = new MySqlDataAdapter(sql, conn); // 将数据填充到dataSet中 DataAdapter.Fill(ds); DataTable td = ds.Tables[0]
public MySqlConnection createConnect() { // 数据连接的基本信息对象 MySqlConnectionStringBuilder scsb = new MySqlConnectionStringBuilder(); scsb.UserID = "root"; scsb.Server = "127.0.0.1"; scsb.Port = 3306; scsb.Password = "123456"; scsb.Database = "account_center"; MySqlConnection mySqlConnection = new MySqlConnection(scsb.ConnectionString); mySqlConnection.Open(); Console.WriteLine("数据库连接成功!"); return mySqlConnection; }
方式二:直接通过字符串,然后new MySqlConnection
public MySqlConnection createConnect2() { // 数据连接的基本信息对象 string conStr = "server=127.0.0.1;database=account_center;username=root;password=123456;"; MySqlConnection mySqlConnection = new MySqlConnection(conStr); mySqlConnection.Open(); return mySqlConnection; }
public void queryNoArg(MySqlConnection conn) { string sql = "select * from user_test "; DataTable dt = new DataTable(); try { MySqlDataAdapter DataAdapter = new MySqlDataAdapter(sql, conn); // 将数据填充到dataTable中 DataAdapter.Fill(dt); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { // 关闭连接 conn.Close(); } // 打印表数据 printData(dt); }
打印数据方法
public void printData(DataTable dt) { if (dt.Rows.Count < 0) { return; } //打印所有列名 string columnName = string.Empty; for (int i = 0; i < dt.Columns.Count; i++) { columnName += dt.Columns[i].ColumnName + " | "; } Console.WriteLine(columnName); Console.WriteLine("-------------------------"); //打印每一行的数据 foreach (DataRow row in dt.Rows) { string columnStr = string.Empty; foreach (DataColumn column in dt.Columns) { columnStr += row[column] + " | "; } Console.WriteLine(columnStr); } }
输出结果
name | age | create_date | ------------------------- 张三 | 12 | 2023/1/4 17:17:24 | 李四 | 33 | 2023/1/4 17:17:24 |
public void queryByArg(MySqlConnection conn) { string sql = "select * from user_test where name=@p1 and age=@p2 "; DataTable dt = new DataTable(); try { // 建立命令执行对象 MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.CommandText = sql; // 设置命令的类型,普通的sql命令是字符串的用Text即可 ,如果是存储过程则用 CommandType.StoredProcedure cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@p1", "张三"); cmd.Parameters.AddWithValue("@p2", 12); MySqlDataAdapter DataAdapter = new MySqlDataAdapter(cmd); // 将数据填充到dataTable中 DataAdapter.Fill(dt); // 释放资源 DataAdapter.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { // 关闭连接 conn.Close(); } // 打印表数据 printData(dt); }
输出结果
name | age | create_date | ------------------------- 张三 | 12 | 2023/1/4 17:17:24 |
public void ExecuteReader(MySqlConnection connection) { string sql = "select * from user_test "; MySqlCommand cmd = new MySqlCommand(sql, connection); MySqlDataReader myReader = null; try { myReader = cmd.ExecuteReader(); // 打印数据 printByReader(myReader); } catch (Exception e) { throw new Exception(e.Message); } finally { connection.Close(); cmd.Dispose(); } }
打印数据方法
public void printByReader(MySqlDataReader myReader) { if (myReader==null) { return; } //打印所有列名 string columnName = string.Empty; for (int i = 0; i < myReader.FieldCount; i++) { columnName += myReader.GetName(i) + " | "; } Console.WriteLine(columnName); Console.WriteLine("-------------------------"); //打印每一行的数据 while (myReader.Read()) { string columnStr = string.Empty; for (int i = 0; i < myReader.FieldCount; i++) { columnStr += myReader[i].ToString() + " | "; } Console.WriteLine(columnStr); } // 释放资源 myReader.Close(); }
输出结果
name | age | create_date | ------------------------- 张三 | 12 | 2023/1/4 17:17:24 | 李四 | 33 | 2023/1/4 17:17:24 |
public int updateData(MySqlConnection conn) { // 添加数据 string addSql = "insert into user_test(name,age,create_date) values('王五',14,now())"; // 修改数据 string updateSql = "update user_test set age=55 where name='李四'"; // 删除数据 string delSql = "delete from user_test where name = '张三'"; MySqlCommand cmd = new MySqlCommand(addSql, conn); try { int rows = cmd.ExecuteNonQuery(); return rows; } catch (Exception e) { conn.Close(); //throw e; Console.WriteLine(e.Message); } finally { cmd.Dispose(); conn.Close(); } return -1; }
开启
提交 -- 正常
回滚 -- 出现问题
public void TransactionTest(MySqlConnection conn) { string sql = "insert into user_test(name,age,create_date) values('bbb',224,now())"; // 开启事务 MySqlTransaction transaction = conn.BeginTransaction(); try { MySqlCommand cmd = new MySqlCommand(sql, conn); cmd.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine(ex.Message); // 事务回滚 transaction.Rollback(); conn.Close(); } finally { if (conn.State != ConnectionState.Closed) { //事务要么回滚要么提交,即Rollback()与Commit()只执行一个 transaction.Commit(); conn.Close(); } } } // 更多**好看的内容**和**好玩的案例**请关注**我的微信公众号: 程序猿知秋**