c#连接数据库

实验过程:

连接数据库

在vs里面找到工具

点击第二个选项,搜索mysql并安装

点击第一个,安装完成即可

增删改查操作:

设置如图的窗体应用

查询表student中的数据

1.

删除编号为3的数据

查询结果

2.

增加一条编号为9的数据

查询结果

3.

将编号为4的数据分数项改为80

查询结果

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 WindowsFormsAppMysql
{
    public partial class Form1 : Form
    {
        private MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
        private MySqlConnection connection;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            builder.UserID = "root";
            builder.Password = "111";
            builder.Server = "localhost";
            builder.Database = "exam";
            connection = new MySqlConnection(builder.ConnectionString);
            connection.Open();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sql = "select * from " + textBox5.Text;
            MySqlCommand cmd = new MySqlCommand(sql, connection);
            MySqlDataReader reader = cmd.ExecuteReader();

            //设置dataGridView的列
            dataGridView1.ColumnCount = 3;
            dataGridView1.ColumnHeadersVisible = true;


            dataGridView1.Columns[0].Name = "编号";
            dataGridView1.Columns[1].Name = "姓名";
            dataGridView1.Columns[2].Name = "分数";

            //根据查询结果像DataGridView中添加数据行
            while (reader.Read())
            {
                int index = this.dataGridView1.Rows.Add();
                this.dataGridView1.Rows[index].Cells[0].Value = reader.GetString("id");
                this.dataGridView1.Rows[index].Cells[1].Value = reader.GetString("name");
                this.dataGridView1.Rows[index].Cells[2].Value = reader.GetString("corse");
            }

            connection.Close();     //关闭连接
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sql = "insert into " + textBox5.Text + " values (" +"'" + textBox1.Text + "','" + textBox2.Text + "'" + ",'" + textBox3.Text + "')";
            textBox4.Text = sql;
            MySqlCommand cmd = new MySqlCommand(sql, connection);
            cmd.ExecuteNonQuery();
            connection.Close();     //关闭连接
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //字段为字符窜时要加上单引号 ''
            string sql = "delete from " + textBox5.Text + " where id=" + textBox1.Text;
            textBox4.Text = sql;
            MySqlCommand cmd = new MySqlCommand(sql, connection);
            cmd.ExecuteNonQuery();
            connection.Close();     //关闭连接
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string sql = "update " + textBox5.Text + " set name=" + "'" + textBox2.Text + "'" + ",corse='" + textBox3.Text +"'" + " where id=" + "'"+textBox1.Text+"'";
            textBox4.Text = sql;
            MySqlCommand cmd = new MySqlCommand(sql, connection);
            cmd.ExecuteNonQuery();
            connection.Close();     //关闭连接
        }

  
    }
}

仓库地址:

https://gitee.com/hanshuai123321/windows-connection-database.git

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值