zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
    }

    效果:

     数据库表:

  • 相关阅读:
    Python 的 IDLE 编辑器
    Android中如何在Eclipse中关联源代码?(图文)
    HTMl5的存储方式sessionStorage和localStorage详解
    HTML的 <u> 标签
    CSS巧妙实现分隔线的几种方法
    关于ajax跨域请求(cross Domain)
    JQuery中$.ajax()方法参数都有哪些?
    最优雅,高效的javascript字符串拼接
    深入学习JavaScript: apply 方法 详解(转)——非常好
    jQuery.ajax() 函数详解
  • 原文地址:https://www.cnblogs.com/my---world/p/12044302.html
Copyright © 2011-2022 走看看