Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何从SQL数据库填充对象列表_C#_Sql_Wpf_Entity Framework_Datagrid - Fatal编程技术网

C#如何从SQL数据库填充对象列表

C#如何从SQL数据库填充对象列表,c#,sql,wpf,entity-framework,datagrid,C#,Sql,Wpf,Entity Framework,Datagrid,我正在WPF中制作这个在线商店,作为我编程任务的一部分,我有一个游戏页面,上面显示所有游戏。我使用了一个DataGrid,它也有一个带有行定义的DataTemplate,这样当你点击一行时,它会展开并向你显示关于你点击的游戏的更多信息。我主要使用文本块来显示前端的内容。所有前端控件在我的.cs文件中都有一个名为“games”的对象列表的数据绑定,我在其中手动添加了一些游戏的详细信息,以便对其进行测试(在测试时,现在重复的是同一个游戏) 我的问题是,我需要从数据库中填充此对象列表的内容,我不知道如

我正在WPF中制作这个在线商店,作为我编程任务的一部分,我有一个游戏页面,上面显示所有游戏。我使用了一个DataGrid,它也有一个带有行定义的DataTemplate,这样当你点击一行时,它会展开并向你显示关于你点击的游戏的更多信息。我主要使用文本块来显示前端的内容。所有前端控件在我的.cs文件中都有一个名为“games”的对象列表的数据绑定,我在其中手动添加了一些游戏的详细信息,以便对其进行测试(在测试时,现在重复的是同一个游戏)

我的问题是,我需要从数据库中填充此对象列表的内容,我不知道如何实现这一点,我的想法是,您可以更改数据库中的游戏数量,您也可以在游戏页面上的datagrid中看到这一点(也就是说,假设您运行一个查询并在DB表中再添加两个游戏,那么DataGrid上还会显示另外两个游戏)。 我已经看了一些填充这个列表的东西,比如使用datatable或者只是制作一个巨大的for循环来为您完成这项工作,但是就我而言,只要我理解它并且它工作,任何类型的解决方案都是非常受欢迎的

这就是我手动存储在列表中的数据的当前外观。就其外观而言,这是理想的结果。我只需要它通过列表中的数据绑定获得的所有数据都来自数据库,而不是由我手动添加到.cs文件中。不要太担心图像,因为它是从URL加载的d我确信,一旦我知道如何从数据库中填充这个列表,我也将能够从数据库中添加图像

下面是前端的XAML代码

<Page xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"  x:Class="RareMantis.GamesPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:RareMantis"
      mc:Ignorable="d" 
      d:DesignHeight="534" d:DesignWidth="1200"
      Title="GamesPage"

      xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
      TextElement.Foreground="{DynamicResource MaterialDesignBody}"
      TextElement.FontWeight="Regular"
      TextElement.FontSize="13"
      TextOptions.TextFormattingMode="Ideal"
      TextOptions.TextRenderingMode="Auto"
      Background="{DynamicResource MaterialDesignPaper}"
      FontFamily="{DynamicResource MaterialDesignFont}">


    <!--Main Grid used for setting blue background-->
    <Grid Background="#374e70">

     <!--Second grid with page content-->
        <Grid>
            <!--Declare Rows-->
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <!--Declare Columns-->
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="170"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <!--Grid containing Page Title-->
            <Grid Grid.RowSpan="2">
                <TextBlock Text="Games" HorizontalAlignment="Center" Margin="10" FontSize="28"/>
            </Grid>

            <!--Page content is placed withing a stackpanel (+scrollviewer)-->
            <ScrollViewer Grid.Column="1" Grid.Row="1" Background="#FFF1F1F1">
                <StackPanel>

                    <!--Catalogue Text-->
                    <TextBlock Text="Catalogue" Margin="10" FontSize="22" FontWeight="Medium"/>

                        <!--Datagrid containig the table with rows and columns-->
                        <DataGrid Name="dg_Games" AutoGenerateColumns="False">

                            <!--Declare the main columns that get displayed initially and bind data from "Game class"-->
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="Title" Binding="{Binding Title}" />
                                <DataGridTextColumn Header="Genre" Binding="{Binding Genre}" />
                                <DataGridTextColumn Header="Release Date" Binding="{Binding Release}" />
                                <DataGridTextColumn Header="Price £" Binding="{Binding Price}" />
                            </DataGrid.Columns>

                            <!--Declare the details contained by the rows(when you click them and they expand)-->
                            <DataGrid.RowDetailsTemplate>
                                <DataTemplate>

                                    <!--Dockpanel for expanding the row-->
                                    <DockPanel Background="GhostWhite">
                                    <Image DockPanel.Dock="Left" Source="{Binding ImageUrl}" Height="64" Margin="10" />

                                    <!--Grid for row dockpanel content-->
                                        <Grid Margin="0,10">

                                            <!--Declare 2 Columns-->
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto" />
                                                <ColumnDefinition Width="*" />
                                            </Grid.ColumnDefinitions>

                                            <!--Declare 4 Rows-->
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto" />
                                                <RowDefinition Height="Auto" />
                                                <RowDefinition Height="Auto" />
                                                <RowDefinition Height="Auto" />
                                            </Grid.RowDefinitions>

                                            <!--Game Image from class-->
                                            <TextBlock Text="" FontWeight="Bold" />
                                            <TextBlock Text="{Binding GameImage}" Grid.Column="1" />

                                            <!--Developer name from class-->
                                            <TextBlock Text="Developer: " FontWeight="Bold" Grid.Row="1" />
                                            <TextBlock Text="{Binding Developer}" Grid.Column="1" Grid.Row="1" />

                                            <!--Description from class-->
                                            <TextBlock Text="Description: " FontWeight="Bold" Grid.Row="2" />
                                            <TextBlock Grid.Column="1" Grid.Row="3" Width="300" HorizontalAlignment="Left" Text="{Binding Description}" TextWrapping="WrapWithOverflow"/>

                                            <!--Purchase button-->
                                            <Button x:Name="btn_Purchase" Grid.Column="1" Grid.Row="3" Width="100" Click="btn_Purchase_Click" Content="Purchase" />

                                        </Grid>

                                    </DockPanel>

                                </DataTemplate>

                            </DataGrid.RowDetailsTemplate>

                        </DataGrid>

                </StackPanel>

            </ScrollViewer>

        </Grid>

    </Grid>

</Page> 

这是.cs文件中的C代码

using System;
using System.Collections.Generic;
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;

//add namespace
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace RareMantis
{
    /// <summary>
    /// Interaction logic for GamesPage.xaml
    /// </summary>
    public partial class GamesPage : Page
    {
        public GamesPage()
        {
            InitializeComponent();

            //Make List based on object items
            List<Game> games = new List<Game>();

            //Add games manally via "Games" class
            games.Add(new Game() { GameID = 1, ImageUrl = "https://www.freepnglogos.com/uploads/gta-5-logo-png/grand-theft-auto-v-1.png", Title = "GTA 5", Genre = "Action-adventure, Open world", Description = "Grand Theft Auto V for PC offers players the option to explore the award-winning world of Los Santos and Blaine County in resolutions of up to 4k and beyond, as well as the chance to experience the game running at 60 frames per second.", Developer = "Rockstar North", Release = new DateTime(2013, 10, 4), Price = "59.99" });
            games.Add(new Game() { GameID = 2, ImageUrl = "https://www.freepnglogos.com/uploads/gta-5-logo-png/grand-theft-auto-v-1.png", Title = "GTA 5", Genre = "Action-adventure, Open world", Description = "Grand Theft Auto V for PC offers players the option to explore the award-winning world of Los Santos and Blaine County in resolutions of up to 4k and beyond, as well as the chance to experience the game running at 60 frames per second.", Developer = "Rockstar North", Release = new DateTime(2013, 10, 4), Price = "59.99" });
            games.Add(new Game() { GameID = 3, ImageUrl = "https://www.freepnglogos.com/uploads/gta-5-logo-png/grand-theft-auto-v-1.png", Title = "GTA 5", Genre = "Action-adventure, Open world", Description = "Grand Theft Auto V for PC offers players the option to explore the award-winning world of Los Santos and Blaine County in resolutions of up to 4k and beyond, as well as the chance to experience the game running at 60 frames per second.", Developer = "Rockstar North", Release = new DateTime(2013, 10, 4), Price = "59.99" });
            games.Add(new Game() { GameID = 4, ImageUrl = "https://www.freepnglogos.com/uploads/gta-5-logo-png/grand-theft-auto-v-1.png", Title = "GTA 5", Genre = "Action-adventure, Open world", Description = "Grand Theft Auto V for PC offers players the option to explore the award-winning world of Los Santos and Blaine County in resolutions of up to 4k and beyond, as well as the chance to experience the game running at 60 frames per second.", Developer = "Rockstar North", Release = new DateTime(2013, 10, 4), Price = "59.99" });
            games.Add(new Game() { GameID = 5, ImageUrl = "https://www.freepnglogos.com/uploads/gta-5-logo-png/grand-theft-auto-v-1.png", Title = "GTA 5", Genre = "Action-adventure, Open world", Description = "Grand Theft Auto V for PC offers players the option to explore the award-winning world of Los Santos and Blaine County in resolutions of up to 4k and beyond, as well as the chance to experience the game running at 60 frames per second.", Developer = "Rockstar North", Release = new DateTime(2013, 10, 4), Price = "59.99" });

            //Add to datagrid from "games list"
            dg_Games.ItemsSource = games;
        }



        //Game class for database content on games page
        public class Game
        {
            public int GameID { get; set; }

            //Not used anymore
            //public Image GameImage { get; set; }

            public string Title { get; set; }

            public string Description { get; set; }

            public string Genre { get; set; }

            public string Developer { get; set; }

            public DateTime Release { get; set; }

            public string Price { get; set; }


            //Temporary, don't worry about this
            public string ImageUrl { get; set; }
        }

        //Button purchase click
        private void btn_Purchase_Click(object sender, RoutedEventArgs e)
        {

        }



    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
//添加名称空间
使用System.Data.Sql;
使用System.Data.SqlClient;
使用系统配置;
使用系统数据;
名称空间RareMantis
{
/// 
///GamesPage.xaml的交互逻辑
/// 
公共部分类游戏页面:第页
{
公众游戏
{
初始化组件();
//基于对象项生成列表
列表游戏=新列表();
//通过“游戏”类手动添加游戏
games.Add(新游戏(){GameID=1,ImageUrl=”https://www.freepnglogos.com/uploads/gta-5-logo-png/grand-theft-auto-v-1.png,Title=“GTA 5”,Genre=“动作冒险,开放世界”,描述=“Grand Theft Auto V for PC为玩家提供了以高达4k或更高分辨率探索屡获殊荣的洛桑托斯和布莱恩县世界的选项,以及体验以每秒60帧运行的游戏的机会。”,Developer=“Rockstar North”,Release=new DateTime(2013,10,4),Price=“59.99”};
games.Add(新游戏(){GameID=2,ImageUrl=”https://www.freepnglogos.com/uploads/gta-5-logo-png/grand-theft-auto-v-1.png,Title=“GTA 5”,Genre=“动作冒险,开放世界”,Description=”Grand Theft Auto V for PC为玩家提供了以高达4k或更高分辨率探索屡获殊荣的洛桑托斯和布莱恩县世界的选项,以及体验以每秒60帧运行的游戏的机会。”,Developer=“Rockstar North”,Release=new DateTime(2013,10,4),Price=“59.99”};
games.Add(新游戏(){Ga
public partial class GamesPage : Page
    {

        RareMantisEntities1 _db = new RareMantisEntities1();
        public static DataGrid datagrid;

        public GamesPage()
        {
            InitializeComponent();
            Load();
        }

        private void Load()
        {
            dg_Games.ItemsSource = _db.Games.ToList();
            datagrid = dg_Games;
        }

    }