c# datagridview控件连接Mysql数据库操作

c# datagridview控件连接Mysql数据库操作

此程序功能小白也可顺利实现

项目需要,开始学习c#,接触到了datagridview控件,此处是连接mysql数据库的操作。

1.拖入控件到winform

在这里插入图片描述
在这里我单独在控件的列里输入了我需要加载数据的表头
点击添加列,然后添加自己要添加的内容,在headertext栏中输入即可。在这里插入图片描述
再就是数据库的生成

我用的是Navicat Premium,点击连接->Mysql->输入连接名和用户名、密码,密码为你的mysql密码,主机名不变,为localhost。
在这里插入图片描述

2.创建数据库

在刚刚成功建立的连接下右键新建数据库,这里的数据库名要记住。在这里插入图片描述

然后添加表,这里不多说。我的表贴在下面

3.代码部分

public partial class ViewForSql : Form
//我的数据库名是metrodataprocess,注意不是连接名!
string connectStr = "server=127.0.0.1;port=3306;user=root;password=199711;database=metrodataprocess;";
public ViewForSql()
        {
            InitializeComponent();
            Adapt();  //使控件宽度自适应
            ConnectView(); //连接数据库操作
        }
        //这段也是我刚从网上拷贝的hhh
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";     //from后面的是自己创建的表
                MySqlCommand cmd = new MySqlCommand(sqlCmd, conn);
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    int index = this.dataGridView1.Rows.Add();
                    this.dataGridView1.Rows[index].Cells[0].Value = reader.GetString("区号");
                    this.dataGridView1.Rows[index].Cells[1].Value = reader.GetString("孔号");
                    this.dataGridView1.Rows[index].Cells[2].Value = reader.GetString("孔深");
                    this.dataGridView1.Rows[index].Cells[3].Value = reader.GetString("组号");
                    this.dataGridView1.Rows[index].Cells[4].Value = reader.GetString("正反测");
                    this.dataGridView1.Rows[index].Cells[5].Value = reader.GetString("最大孔深");
                    this.dataGridView1.Rows[index].Cells[6].Value = reader.GetString("电压");
                    this.dataGridView1.Rows[index].Cells[7].Value = reader.GetString("位移");
                    this.dataGridView1.Rows[index].Cells[8].Value = reader.GetString("时间");
                }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
       }

下面是我的数据表格式

在这里插入图片描述

显示结果

在这里插入图片描述
控件右边显示的还是不完美,明天我再看看。

或者直接在代码部分操作datagridview的数据源

只需将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;
  • 11
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
DataGridView是Windows Forms中的一个控件,用于在表格中显示和编辑数据。MySQL是一种关系型数据库管理系统,用于存储和管理数据。 要在DataGridView中显示MySQL中的数据,你可以使用MySQL连接库(如MySQL Connector/NET)来连接数据库,并执行查询获取数据。然后,可以将查询结果绑定到DataGridView的DataSource属性上,以便在表格中显示数据。 下面是一个简单的示例代码,演示如何使用DataGridViewMySQL连接库来显示数据: ```csharp using MySql.Data.MySqlClient; // 创建MySQL连接字符串 string connectionString = "server=localhost;database=mydatabase;uid=username;password=password"; // 创建MySQL连接 using (MySqlConnection connection = new MySqlConnection(connectionString)) { // 打开数据库连接 connection.Open(); // 创建查询命令 MySqlCommand command = new MySqlCommand("SELECT * FROM mytable", connection); // 创建数据适配器 MySqlDataAdapter adapter = new MySqlDataAdapter(command); // 创建数据表 DataTable table = new DataTable(); // 填充数据表 adapter.Fill(table); // 将数据表绑定到DataGridView dataGridView1.DataSource = table; } ``` 以上代码假设你已经在窗体上添加了一个DataGridView控件,并将其命名为dataGridView1。你需要根据自己的实际情况修改连接字符串、查询语句以及控件名称。 通过这个示例,你可以在DataGridView中显示MySQL中的数据,并且还可以进行排序、筛选、编辑等操作
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值