使用ADO技术对Mysql进行增删查改(C#)

  我是在VS2015进行代码编写的,正常情况下,代码应该是通用的。

  我知道的在VS2015中连接mysql数据库的方法一共有2种。

  1.下载SQLServer,通过SQLServer进行数据库的连接。

  2.需要在VS中下载一个名为:MySql.Data的NuGet扩展包,不过有可能最新无法安装,你可以先安装较低版本的扩展,然后更新到最新版本,我之前是遇到过这个情况。

  之后,还需要安装mysql-connector-net和mysql-for-visualstudio这两个用于vs连接mysql的扩展包,安装步骤就是一路next就行,版本选typical就行,这两个文件我之前上传过地址是https://download.csdn.net/download/qq_43238335/12411009。然后就是连接数据库,数据库选择MySQL database。

 最后就是,写程序的时候需要把MySql.Data加入引用

  我用的是第二种方法,因为第一种还要下载安装,而且国外软件还不好搞。第二种方法对我来说简直是懒癌患者的福音,片刻思考之后,我果断选了第二种方法。

  对于C#程序,我用的是窗体程序,这样做既简单又直观,耗费的脑力体力比较少,毕竟对于宅男来说,脑力还是蛮重要的。

  我将查询的结果填充到数据适配器,然后再dataGridView中显示,对于添加、删除、修改操作,如果操作成功的话,会通过消息框(MessageBox)显示。

  方法并不难,主要是将数据库连接语句和数据库操作语句写对,写对了基本就无问题了。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace 数据库
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connectionString = "server=localhost;user id=root;database=bbb;password=2411030483";
            MySqlConnection connection = new MySqlConnection(connectionString);
            MySqlCommand command = new MySqlCommand("select * from student", connection);
            connection.Open();
            MySqlDataAdapter adapter = new MySqlDataAdapter(command);
            DataTable dataTable = new DataTable();
            adapter.Fill(dataTable);
            dataGridView1.DataSource = dataTable;
            connection.Close();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //修改
            string connectionString = "server=localhost;user id=root;database=bbb;password=2411030483";
            MySqlConnection connection = new MySqlConnection(connectionString);
            connection.Open();
            string sql = "update student set sex='" + textBox3.Text + "'  where name='"+this.textBox2.Text+"'";
            MySqlCommand command = new MySqlCommand(sql, connection);
            int result = command.ExecuteNonQuery();
            MessageBox.Show("更新信息成功");
            connection.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //插入
            string connectionString = "server=localhost;user id=root;database=bbb;password=2411030483";
            MySqlConnection connection = new MySqlConnection(connectionString);
            connection.Open();
            string sql = "insert into student(name,sex) values ('" + this.textBox2.Text+ "','" + this.textBox3.Text + "')";
            MySqlCommand command = new MySqlCommand(sql, connection);
            int result = command.ExecuteNonQuery();
            MessageBox.Show("插入信息成功");
            connection.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //删除
            string connectionString = "server=localhost;user id=root;database=bbb;password=2411030483";
            MySqlConnection connection = new MySqlConnection(connectionString);
            connection.Open();
            string sql = "delete from student where name='" + this.textBox2.Text + "' and sex='" + this.textBox3.Text + "'";
            MySqlCommand command = new MySqlCommand(sql, connection);
            int result = command.ExecuteNonQuery();
            MessageBox.Show("删除信息成功");
            connection.Close();
        }
    }
}

  窗体如下:

 窗体类型有:label、 textbox、button、dataGridView

猜你喜欢

转载自blog.csdn.net/qq_43238335/article/details/106334786
今日推荐