打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C#窗体连接MySql并通过DataGridView展示数据

第一步,添加MySql.Data工具,

首先,C#连接MySql数据库需要用到C#连接MySql数据库所用到的动态链接库--MySql.Data,如果没有这个文件首先我们需要将他添加进项目中,

1.右键项目名,点击管理NuGet程序包:

 

 

 

2.在浏览页面的搜索栏输入MySql.Data,如果没有安装右侧会有安装一栏选项,我们就可以点击右侧的安装选项进行安装,安装成功后我们就可以进行编码操作了:

 

 

 第二步,编码实现,

 然后,我们就可以进入编码阶段了,

首先我们需要加入头文件:

using MySql.Data.MySqlClient;

 

 

这样我们就可以使用MySql.Data中的方法来连接数据库了,连接数据库代码如下:

             String connetStr = "server=127.0.0.1;port=3306;user=root;password=123; database=vs;";            //usr:用户名,password:数据库密码,database:数据库名            MySqlConnection conn = new MySqlConnection(connetStr);            try            {                conn.Open();//打开通道,建立连接,可能出现异常,使用try catch语句                Console.WriteLine("已经建立连接");               }            catch (MySqlException ex)            {                Console.WriteLine(ex.Message);            }            finally            {                conn.Close();            }

如果连接数据库成功,我们就可以进行下面的操作了,取出数据并通过DataGridView展示出来了,代码如下:

             String connetStr = "server=127.0.0.1;port=3306;user=root;password=123; database=vs;";                        MySqlConnection conn = new MySqlConnection(connetStr);            try            {                conn.Open();//打开通道,建立连接,可能出现异常,使用try catch语句                Console.WriteLine("已经建立连接");
string sql = "select * from salecar"; MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader();//执行ExecuteReader()返回一个MySqlDataReader对象 while (reader.Read()) { int index = this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].Cells[0].Value = reader.GetString("name"); this.dataGridView1.Rows[index].Cells[1].Value = reader.GetString("describe"); this.dataGridView1.Rows[index].Cells[2].Value = reader.GetString("price"); this.dataGridView1.Rows[index].Cells[3].Value = reader.GetInt32("salenumber"); } } catch (MySqlException ex) { Console.WriteLine(ex.Message); } finally { conn.Close(); }

这样我们就完成了C#窗体连接MySql并通过DataGridView展示数据,下面是效果图和全部代码:

全部代码:

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 WindowsFormsApp1{    public partial class Form3 : Form    {        public Form3()        {            InitializeComponent();            a();        }        private void button1_Click(object sender, EventArgs e)        {            Form1 fm1 = new Form1();            this.Hide();            fm1.ShowDialog();            Application.ExitThread();        }        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)        {                    }        public void a()        {                        String connetStr = "server=127.0.0.1;port=3306;user=root;password=123; database=vs;";            MySqlConnection conn = new MySqlConnection(connetStr);            try            {                conn.Open();//打开通道,建立连接,可能出现异常,使用try catch语句                Console.WriteLine("已经建立连接");                //在这里使用代码对数据库进行增删查改                string sql = "select * from salecar";                MySqlCommand cmd = new MySqlCommand(sql, conn);                MySqlDataReader reader = cmd.ExecuteReader();//执行ExecuteReader()返回一个MySqlDataReader对象                while (reader.Read())                {                    int index = this.dataGridView1.Rows.Add();                              this.dataGridView1.Rows[index].Cells[0].Value = reader.GetString("name");                    this.dataGridView1.Rows[index].Cells[1].Value = reader.GetString("describe");                    this.dataGridView1.Rows[index].Cells[2].Value = reader.GetString("price");                    this.dataGridView1.Rows[index].Cells[3].Value = reader.GetInt32("salenumber");                  }            }            catch (MySqlException ex)            {                Console.WriteLine(ex.Message);            }            finally            {                conn.Close();            }        }    }}

效果:

 

 数据库表:

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
连接SQL Server数据库
C#连接MySQL,并完成对数据库的增删查改
C#连接MySQL数据库实例
DataGridView控件读写MySQL数据库表格的方法
c# mysql数据库连接(我碰到的问题)
java之JDBC(MySql)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服