ADO.NET 操作 Mysql
ADO.NET 操作Mysql
首先要通过Nuget安装mysql.data包,
连接字符串:
public static string MysqlCnnectionString = "Server =localhost; Database =test; Uid =root; Pwd =mrf@2017;Pooling=true; Max Pool Size=20;Min Pool Size=10;Allow Batch=true";
或者 放在配置文件中:
插入:
static void MysqlInsert() { var sql = @"INSERT table_a ( name, create_time, parant_id ) VALUES ( 'test', '2017-01-01', 1 )"; using (MySqlConnection conn = new MySqlConnection(MysqlCnnectionString)) { conn.Open(); MySqlHelper.ExecuteNonQuery(conn,sql); } }
带参数参入
static void MysqlInsertByParameters() { var sql = @"INSERT table_a ( name, create_time, parant_id ) VALUES ( @name, @create_time, @parant_id )"; MySqlParameter[] parameter = new MySqlParameter[3]; parameter[0] = new MySqlParameter("name", "test"); parameter[1] = new MySqlParameter("create_time", DateTime.Now); parameter[2] = new MySqlParameter("parant_id", 1); using (MySqlConnection conn = new MySqlConnection(MysqlCnnectionString)) { conn.Open(); MySqlHelper.ExecuteNonQuery(conn, sql, parameter); } }
修改:
static void MysqlUpdate() { var sql = "UPDATE table_a SET name = '1111' where id = 1"; using (MySqlConnection conn = new MySqlConnection(MysqlCnnectionString)) { conn.Open(); MySqlHelper.ExecuteNonQuery(conn, sql); } }
删除:
static void MysqlDelete() { var sql = "delete from table_a where id = 1"; using (MySqlConnection conn = new MySqlConnection(MysqlCnnectionString)) { conn.Open(); MySqlHelper.ExecuteNonQuery(conn, sql); } }
查询, 后期补...