Android类微信界面

MySQL

// An highlighted block
var foo = 'bar';
```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 MySQL
{
    public partial class Form1 : Form
    {
        string connectStr = "server=127.0.0.1;port=3306;user=root;password=123456;database=chen;";

        public object mda { get; private set; }

        public Form1()
        {
            InitializeComponent();
            Adapt();  //使控件宽度自适应
            ConnectView(); //连接数据库操作
        }

        public void Adapt()
        {
            int width = 0;
            for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
            {
                this.dataGridView1.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells);
                width += this.dataGridView1.Columns[i].Width;
            }
            if (width > this.dataGridView1.Size.Width)
            {
                this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            }
            else
            {
                this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
        }

        public void ConnectView()
        {
            MySqlConnection conn = new MySqlConnection(connectStr); //新建连接
            try
            {

                conn.Open();
                MessageBox.Show("已经建立连接");
                string sqlCmd = "select * from inclinometer";
                MySqlCommand cmd = new MySqlCommand(sqlCmd, conn);
                MySqlDataReader reader = cmd.ExecuteReader();

                BindingSource bs = new BindingSource();
                bs.DataSource = reader;
                this.dataGridView1.DataSource = bs;

            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }


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

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string M_str_sqlcon = "server=localhost;user id=root;password=123456;database=mysql2022";                                                                                              //创建数据库连接对象
            conn = new MySqlConnection(M_str_sqlcon);
            try
            {
                //打开数据库连接
                conn.Open();
                MessageBox.Show("数据库连接成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

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

        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, "user");
                    MessageBox.Show("添加成功", "提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "error!");
            }

        }

        private void button4_Click(object sender, EventArgs e, object ds)
        {
            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, "user");
                    MessageBox.Show("修改成功", "提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "error!");
            }
        }

        private void button5_Click(object sender, EventArgs e, object conn)
        {
            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("error!");
                return;
            }
            conn.Close();
        }

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


//


https://gitee.com/chenrrr/my-sql
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值