WPF连接mysql数据库查询展示数据

效果:

在这里插入图片描述

源码

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using MySql.Data.MySqlClient;


namespace WpfApp2
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            string MyConStr = "Server=localhost;Database=aaa;uid=root;pwd=123456";

            MySqlConnection conn = new MySqlConnection(MyConStr);

            conn.Open();

            if (conn.State == ConnectionState.Open)
            {
                MessageBox.Show("Connection Opened Successfully");
                conn.Close();
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DBConnection conn = new DBConnection();
            // DataSet dataset = conn.Select();
            DataTable dataset = conn.Select();
            if (dataset == null)
            {
                MessageBox.Show("没有数据");
            }
            Console.WriteLine("结果:" + dataset);
            DataGrid1.ItemsSource = dataset.DefaultView;
            // 原文链接:https://blog.youkuaiyun.com/qq_43026206/article/details/86706431
        }
    }

    class DBConnection
    {
        private MySqlConnection connection;
        private string connectionStr;

        public DBConnection()
        {
            try
            {
                string MyConStr = "Server=localhost;Database=aaa;uid=root;pwd=123456";
                /*connectionStr = ConfigurationManager.ConnectionStrings[MyConStr].ConnectionString;
                connection = new MySqlConnection(connectionStr);*/

                connection = new MySqlConnection(MyConStr);
            }
            catch (Exception exception)
            {
                throw new Exception("DBConnection Constructor:" + exception.Message);
            }
        }

        public DataTable Select()
        {
            try
            {
                connection.Open();
                /*if (connection != null)
                {
                    connection.Open();
                }*/

                Console.WriteLine("---开始查询--");
                string cmdStr = "Select * from students";
                MySqlCommand sqlCmd = new MySqlCommand(cmdStr, connection);
                MySqlDataAdapter sda = new MySqlDataAdapter(sqlCmd);

                DataTable ds = new DataTable();
                sda.Fill(ds);
                return ds;
            }
            catch (Exception exception)
            {
                throw new Exception("SelectMethod:" + exception.Message);
            }
            finally
            {
                connection.Close();
            }
        }
    }
    // 原文链接:https://blog.youkuaiyun.com/qq_43026206/article/details/86706431
}

不懂的话,可以加我微信号yizheng369

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值