c#第三次作业 连接数据库

using MySql.Data.MySqlClient;
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;


namespace WindowsFormsApp13
{
    public partial class Form1 : Form
    {
        MySqlConnection conn; //连接数据库对象
        MySqlDataAdapter mda; //适配器变量
        DataSet ds;  //临时数据集

        public Form1()
        {
            InitializeComponent();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string M_str_sqlcon = "server=localhost;user id=root;password=a2951705151;port=3306;persistsecurityinfo=True;database=test;SslMode=None";                                                                                             //创建数据库连接对象
            conn = new MySqlConnection(M_str_sqlcon);
            try
            {
                //打开数据库连接
                conn.Open();
                MessageBox.Show("数据库已经连接了!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
         }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "select * from data1";
            mda = new MySqlDataAdapter(sql, conn);
            ds = new DataSet();
            mda.Fill(ds, "data1");
            //显示数据
            dataGridView1.DataSource = ds.Tables["data1"];
            conn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (mda == null || ds == null)
            {
                MessageBox.Show("请先导入数据");
                return;
            }
            try
            {
                string msg = "确实要添加此条数据吗?";
                if (1 == (int)MessageBox.Show(msg, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation))
                {
                    MySqlCommandBuilder builder = new MySqlCommandBuilder(mda);
                    mda.Update(ds, "data1");
                    MessageBox.Show("添加成功", "提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "错误信息");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (mda == null || ds == null)
            {
                MessageBox.Show("请先导入数据");
                return;
            }
            try
            {
                string msg = "确实要修改吗?";
                if (1 == (int)MessageBox.Show(msg, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation))
                {
                    MySqlCommandBuilder builder = new MySqlCommandBuilder(mda); //命令生成器。
                                                                                //适配器会自动更新用户在表上的操作到数据库中
                    mda.Update(ds, "data1");
                    MessageBox.Show("修改成功", "提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "错误信息");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int index = dataGridView1.CurrentCell.RowIndex;
            int id = (int)dataGridView1.Rows[index].Cells[0].Value;
            string sql = "delete from user where id=" + id + "";
            conn.Open();
            MySqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = sql;
            int i = cmd.ExecuteNonQuery();
            if (i < 0)
            {
                conn.Close();
                MessageBox.Show("删除失败");
                return;
            }
            conn.Close();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

 

小结:通过这次实验,我对使用vs进行MySQL数据库的连接有了一定的了解,尽管在实验过程中,我在写代码时出现了许多bug,而且多次连接数据库都没有成功,但经过了多次实验,最终得出了正确的结果。

gitee地址:https://gitee.com/zhu-yujie123/task-3.git 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值