夜光带你走进C#连接mysql数据库(二十二)擅长的领域

mac2024-05-19  26

夜光序言:

 

 

于千万人之中,遇见你要遇见的人。

于千万年之中,时间无涯的荒野里,没有早一步,也没有迟一步,遇上了也只能轻轻地说一句:哦,你也在这里吗?

 

 

 

 

 

 

 

 

 

 

 

正文:

首先:找到mysql文件夹里面的这个文件

 

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace WindowsFormsApp2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } static string conStr = "server=127.0.0.1;user=root;password=1111; database=stu_db;"; MySqlConnection conn = new MySqlConnection(conStr); private void Form1_Load(object sender, EventArgs e) { conn.Open(); MessageBox.Show("连接完成~"); /* Update();*/ conn.Close(); } } }

上图,我们把id为1的数据给删除掉了

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace WindowsFormsApp3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } static string conStr = "server=127.0.0.1;user=root;password=1111; database=stu_db;"; MySqlConnection conn = new MySqlConnection(conStr); private void Form1_Load_1(object sender, EventArgs e) { conn.Open(); MessageBox.Show("连接完成~"); MyUpdate(); conn.Close(); } void MyUpdate() { /* string sql = "update students set 姓名='Genius Team' where 学号='001'";*/ /* string sql = "SELECT * FROM students";*/ string sql = "DELETE FROM user WHERE id=1"; MySqlCommand mys = new MySqlCommand(sql, conn); int result = mys.ExecuteNonQuery(); /* textBox1.Text = result.ToString();*/ if (result != 0) { MessageBox.Show("完成"); } } } }

 

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace WindowsFormsApp3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } static string conStr = "server=127.0.0.1;user=root;password=1111; database=stu_db;"; MySqlConnection conn = new MySqlConnection(conStr); private void Form1_Load_1(object sender, EventArgs e) { conn.Open(); MessageBox.Show("连接完成~"); /* MyDelete();*/ MyInsert(); conn.Close(); } //夜光:删除方法 void MyDelete() { /* string sql = "update students set 姓名='Genius Team' where 学号='001'";*/ /* string sql = "SELECT * FROM students";*/ string sql = "DELETE FROM user WHERE id=1"; MySqlCommand mys = new MySqlCommand(sql, conn); int result = mys.ExecuteNonQuery(); /* textBox1.Text = result.ToString();*/ if (result != 0) { MessageBox.Show("夜光:删除完成"); } } //夜光:更新方法 void MyInsert() { /* string sql = "update students set 姓名='Genius Team' where 学号='001'";*/ /* string sql = "SELECT * FROM students";*/ string sql = "insert into user(id,name,pwd) values(10,'aa','111')"; MySqlCommand mys = new MySqlCommand(sql, conn); int result = mys.ExecuteNonQuery(); /* textBox1.Text = result.ToString();*/ if (result != 0) { MessageBox.Show("夜光:新增完成"); } } } }

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace WindowsFormsApp3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } static string conStr = "server=127.0.0.1;user=root;password=1111; database=stu_db;"; MySqlConnection conn = new MySqlConnection(conStr); private void Form1_Load_1(object sender, EventArgs e) { conn.Open(); MessageBox.Show("连接完成~"); /*MyDelete();*/ /*MyInsert();*/ MyQuery(); conn.Close(); } //夜光:删除方法 void MyDelete() { /* string sql = "update students set 姓名='Genius Team' where 学号='001'";*/ /* string sql = "SELECT * FROM students";*/ string sql = "DELETE FROM user WHERE id=1"; MySqlCommand mys = new MySqlCommand(sql, conn); int result = mys.ExecuteNonQuery(); /* textBox1.Text = result.ToString();*/ if (result != 0) { MessageBox.Show("夜光:删除完成"); } } //夜光:更新方法 void MyInsert() { /* string sql = "update students set 姓名='Genius Team' where 学号='001'";*/ /* string sql = "SELECT * FROM students";*/ string sql = "insert into user(id,name,pwd) values(10,'aa','111')"; MySqlCommand mys = new MySqlCommand(sql, conn); int result = mys.ExecuteNonQuery(); /* textBox1.Text = result.ToString();*/ if (result != 0) { MessageBox.Show("夜光:新增完成"); } } //夜光:查找方法 void MyQuery() { /* string sql = "update students set 姓名='Genius Team' where 学号='001'";*/ /* string sql = "SELECT * FROM students";*/ string sql = "SELECT * FROM user where id=2"; MySqlCommand mys = new MySqlCommand(sql, conn); int result = mys.ExecuteNonQuery(); textBox1.Text = mys.ToString(); if (result != 0) { MessageBox.Show("夜光:查询完成"); } } } }

 

 

 

 

 

 

 

最新回复(0)