作业3 数据库连接1、请设计一个项目连接到自己的MySQL数据库,数据库包含至少三张表;2、使用dataGridView控件显示表中的数据;3、实现基本crud操作;

1.

 

 2.Form.cs代码:

using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“homeworkDataSet.spj”中。您可以根据需要移动或删除它。
            //this.spjTableAdapter.Fill(this.homeworkDataSet.spj);
            // TODO: 这行代码将数据加载到表“homeworkDataSet.s”中。您可以根据需要移动或删除它。
            //this.sTableAdapter.Fill(this.homeworkDataSet.s);
            // TODO: 这行代码将数据加载到表“homeworkDataSet.student”中。您可以根据需要移动或删除它。
            this.studentTableAdapter.Fill(this.homeworkDataSet.student);

        }
    }
}

结果截图:

 3.核心代码:

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 MyDataBase
{
    public partial class Form1 : Form
    {

        private MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
        private MySqlConnection connection;

        public Form1()
        {
            InitializeComponent();
        }

        //获取连接,每次操作前需要打开连接
        private void button7_Click(object sender, EventArgs e)
        {
            builder.UserID = "root";
            builder.Password = "17182715523Skx";
            builder.Server = "localhost";
            builder.Database = "homework";
            connection = new MySqlConnection(builder.ConnectionString);
            connection.Open();

        }

        private void read_Click(object sender, EventArgs e)
        {
            
        }

        private void create_Click(object sender, EventArgs e)
        {
           
        }

        private void delete_Click(object sender, EventArgs e)
        {
            
        }

        //根据编号修改
        private void update_Click(object sender, EventArgs e)
        {
            
        }

        private void excuteNoQuery(string sql)
        {
            textBox8.Text = sql;
            MySqlCommand cmd = new MySqlCommand(sql, connection);
            cmd.ExecuteNonQuery();
            connection.Close();     //关闭连接
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button9_Click(object sender, EventArgs e)
        {
            //字段为字符窜时要加上单引号 ''
            string sql = "insert into c_sharp values (" + textBox5.Text + ",'" + textBox6.Text + "'" +textBox7.Text "," + textBox8.Text + ")";
            excuteNoQuery(sql);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            string sql = "delete from c_sharp where s_id=" + textBox5.Text;
            excuteNoQuery(sql);
        }

        private void 打开连接_Click(object sender, EventArgs e)
        {
            string sql = "update c_sharp set s_name=" + "'" + textBox6.Text + "'" + ",s_birth=" + textBox7.Text + " where s_id=" + textBox5.Text;
            excuteNoQuery(sql);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            //先清空数据
            dataGridView2.Rows.Clear();

            string sql = "select * "student";
            textBox8.Text = sql;
            MySqlCommand cmd = new MySqlCommand(sql, connection);
            MySqlDataReader reader = cmd.ExecuteReader();

            //设置dataGridView的列
            dataGridView2.ColumnCount = 4;
            dataGridView2.ColumnHeadersVisible = true;

            //设置字体
            DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
            columnHeaderStyle.BackColor = Color.Beige;
            columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Bold);
            dataGridView2.ColumnHeadersDefaultCellStyle = columnHeaderStyle;

            dataGridView2.Columns[0].Name = "s_name";
            dataGridView2.Columns[1].Name = "s_birth";
            dataGridView2.Columns[2].Name = "s_id";

            dataGridView2.Columns[3].Name = "s_sex";

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

                this.dataGridView2.Rows[index].Cells[2].Value = reader.GetInt32("s_sex");
            }
            connection.Close();     //关闭连接
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
 

结果截图:

 仓库地址:https://gitee.com/song-kexinxxx/my-storehouse.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值