如何从mysql填充datagrid

dwthyt8l  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(391)

用mysql工作台数据库中的数据填充datagrid最简单的方法是什么?数据库名称为pop-sf40-database,如下所示:

现在只有一个沙龙我手动插入(本地主机(根)
型号:salon.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace POP_SF_40_2014.Model
{
    class Salon
    {
        private int id;
        private string name;
        private string adress;
        private string telephone;
        private string email;
        private string web_site;
        private string tin;
        private string uid;
        private string bank_account;
        private bool deleted;

    }
}

图形用户界面窗口.xaml

<Window x:Class="POP_SF_40_2014.GUI.SalonWindow"
        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:POP_SF_40_2014.GUI"
        mc:Ignorable="d"
        Title="Салони" Height="500" Width="600">
    <Grid>
        <DataGrid x:Name="dgSalons" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="100" Width="572" SelectionChanged="dataGrid_SelectionChanged" LoadingRow="dgSalons_LoadingRow">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ИД" Binding="{Binding Id}" Width="*"/>
                <DataGridTextColumn Header="Назив" Binding="{Binding Name}" Width="*"/>
                <DataGridTextColumn Header="Адреса" Binding="{Binding Adress}" Width="*"/>
                <DataGridTextColumn Header="Телефон" Binding="{Binding Email}" Width="*"/>
                <DataGridTextColumn Header="Интернет адреса" Binding="{Binding WebSite}" Width="*"/>
                <DataGridTextColumn Header="ПИБ" Binding="{Binding Tin}" Width="*"/>
                <DataGridTextColumn Header="Матични Број" Binding="{Binding Uid}" Width="*"/>
                <DataGridTextColumn Header="Жиро рачун" Binding="{Binding BankAccount}" Width="*"/>
            </DataGrid.Columns>
        </DataGrid>
        <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="285" Margin="15,119,0,0" Stroke="Black" VerticalAlignment="Top" Width="345"/>

        <Label x:Name="lblSalonId" Content="ИД" HorizontalAlignment="Left" Margin="15,126,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonId" HorizontalAlignment="Left" Height="23" Margin="123,126,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210" IsReadOnly="True"/>

        <Label x:Name="lblSalonName" Content="Назив" HorizontalAlignment="Left" Margin="15,152,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonName" HorizontalAlignment="Left" Height="23" Margin="123,155,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonAdress" Content="Адреса" HorizontalAlignment="Left" Margin="15,183,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonAdress" HorizontalAlignment="Left" Height="23" Margin="123,186,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonTelephone" Content="Телефон" HorizontalAlignment="Left" Margin="15,214,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonTelephone" HorizontalAlignment="Left" Height="23" Margin="123,217,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonEmail" Content="Е-пошта" HorizontalAlignment="Left" Margin="15,245,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonEmail" HorizontalAlignment="Left" Height="23" Margin="123,248,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonSite" Content="Интернет адреса" HorizontalAlignment="Left" Margin="15,276,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonSite" HorizontalAlignment="Left" Height="23" Margin="123,279,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonTin" Content="ПИБ" HorizontalAlignment="Left" Margin="15,307,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonTin" HorizontalAlignment="Left" Height="23" Margin="123,310,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonUid" Content="Матични број" HorizontalAlignment="Left" Margin="15,338,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonUid" HorizontalAlignment="Left" Height="23" Margin="123,341,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonBankAccount" Content="Жиро рачун" HorizontalAlignment="Left" Margin="15,369,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonBankAccount" HorizontalAlignment="Left" Height="23" Margin="123,369,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Label x:Name="lblSalonSearch" Content="Претражи" HorizontalAlignment="Left" Margin="15,417,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtSalonSearch" HorizontalAlignment="Left" Height="23" Margin="123,420,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="210"/>

        <Button x:Name="btnAdd" Content="Додај" HorizontalAlignment="Left" Margin="365,119,0,0" VerticalAlignment="Top" Width="217"/>
        <Button x:Name="btnUpdate" Content="Измени" HorizontalAlignment="Left" Margin="365,146,0,0" VerticalAlignment="Top" Width="217"/>
        <Button x:Name="btnDelete" Content="Избриши" HorizontalAlignment="Left" Margin="365,173,0,0" VerticalAlignment="Top" Width="217"/>
        <Button x:Name="btnSave" Content="Сачувај" HorizontalAlignment="Left" Margin="365,382,0,0" VerticalAlignment="Top" Width="217"/>
        <Button x:Name="btnSearch" Content="Претражи" HorizontalAlignment="Left" Margin="365,421,0,0" VerticalAlignment="Top" Width="217"/>

    </Grid>
</Window>

看起来是这样的:

另外,这是appconfig

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="POP_SF_40_2014.Properties.Settings.pop_sf40_databaseConnectionString1"
            connectionString="server=localhost;user id=root;database=pop-sf40-database"
            providerName="MySql.Data.MySqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>
m528fe3b

m528fe3b1#

看来您必须为数据访问创建一个类。从体系结构上讲,这将放在应用程序的数据访问层(dal)上。
您必须使用此对象从数据库中检索信息并填充salon对象。如果您使用的是datagrid,那么可能意味着您将处理salon对象的列表(observablecollection)。
这里有一个写dal的最佳实践的好线索

相关问题