WPF read data from mysql and display via ADO.NET

//xaml
<Window x:Class="WpfApp216.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp216"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <DataGrid x:Name="dg"/>
    </Grid>
</Window>

//cs
using System;
using System.Collections.Generic;
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;
using MySql.Data.MySqlClient;

namespace WpfApp216
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MySQLReadWriteUpdateDelete();
        }

        private void MySQLReadWriteUpdateDelete()
        {
            string connStr = "Server=localhost;Database=information_schema;Uid=Uidvalue;Pwd=PwdValue;";
            using(MySqlConnection conn=new MySqlConnection(connStr))
            {
                conn.Open();
                string selectStr = "select * from information_schema.engines;";
                using (MySqlDataAdapter adapter = new MySqlDataAdapter(selectStr, conn))
                {
                    DataSet ds = new DataSet();
                    adapter.Fill(ds);
                    int rowsCount = ds.Tables[0].Rows.Count;
                    int colsCount = ds.Tables[0].Columns.Count;
                    List<MySqlModel> modelsList = new List<MySqlModel>();
                    for(int i=0;i<rowsCount;i++)
                    {
                        modelsList.Add(new MySqlModel()
                        {
                            ENGINE = ds.Tables[0].Rows[i][0].ToString(),
                            SUPPORT = ds.Tables[0].Rows[i][1].ToString(),
                            COMMENT = ds.Tables[0].Rows[i][2].ToString(),
                            TRANSACTIONS = ds.Tables[0].Rows[i][3].ToString(),
                            XA = ds.Tables[0].Rows[i][4].ToString(),
                            SAVEPOINTS = ds.Tables[0].Rows[i][5].ToString(),
                        });
                    }
                    dg.ItemsSource = modelsList;
                }
            }
        }
    }

    public class MySqlModel
    {
        public string ENGINE { get; set; }
        public string SUPPORT { get; set; }
        public string COMMENT { get; set; }
        public string TRANSACTIONS { get; set; }
        public string XA { get; set; }
        public string SAVEPOINTS { get; set; }
    }
}

 

 

 

 

 

posted @ 2024-07-16 20:48  FredGrit  阅读(4)  评论(0编辑  收藏  举报