从正常列表数据转换到wpf xaml中的数据库 [英] Converting from normal list data to a database in wpf xaml

查看:284
本文介绍了从正常列表数据转换到wpf xaml中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在尝试修复我的系统中的许多错误一段时间,我几乎接近完成。其中一个错误导致我创建一个简单的列表,它相当于我的数据库字段。我们都知道,这对于大量的数据来说是愚蠢的 - 现在我已经完成了测试数据,现在是实现数据库连接的时候了。

So I have been trying to fix many errors in my system for a while and I'm pretty close to being done. One of these errors resulted in me creating a simple list that is the equivalent of my database fields. As we all know, this is is stupid for a lot of data - and now I've finished with test data, it's time to implement a database connection.

是我使用的代码文件和与他们的描述:

So below are the code files that I am using and descriptions with them:

DBDisplayViewModel.cs

public class DBDisplayViewModel
{
    public ICollectionView Users { get; private set; }

    /// <summary>
    /// To pull the database
    /// </summary>
    private static string connectionString = Properties.Settings.Default.DBUsers;
    public DBDisplayViewModel()
    {
        //TODO Database connection

         #Region old code
     var _users = new List<User>
                             {
                                 new User
                                     {
                                         ID = "IDTest",
                                         FirstName = "FNTest",
                                         LastName = "LNTest",
                                     },
        #endregion


        Users = CollectionViewSource.GetDefaultView(_users);
    }

}

.cs

    public class Patients : INotifyPropertyChanged
{
    private string _id;
    private string _firstName;
    private string _lastName;

    /// <summary>
    /// Define ID
    /// </summary>
    public string ID
    {
        get { return _id; }
        set
        {
            _id = value;
            NotifyPropertyChanged("ID");
        }
    }

    /// <summary>
    /// Define First Name
    /// </summary>
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            _firstName = value;
            NotifyPropertyChanged("FirstName");
        }
    }

    /// <summary>
    /// Define Last Name
    /// </summary>
    public string LastName
    {
        get { return _lastName; }
        set
        {
            _lastName = value;
            NotifyPropertyChanged("LastName");
        }
    }



    /// <summary>
    /// INotifyPropertyChanged Members
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;


    /// <summary>
    /// Private Helpers
    /// </summary>
    /// <param name="propertyName"></param>
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

DBDisplay.xaml 只有一个绑定(需要连接到数据库)

DBDisplay.xaml simply has a binding (that needs to connect to the DB)

<DataGrid ItemsSource="{Binding Users}"

DBDisplay.xaml .cs 只有ViewModel在

DBDisplay.xaml.cs simply has the ViewModel in

 public DBDisplay()
    {
        InitializeComponent();
        // Pull in the ViewModel
        DataContext = new DBDisplayViewModel();
    }

所以从我可以看到,在View模型中, c $ c> #Region 是,我需要连接到我的(预先存在的)数据库, DBUsers

So from what I can see, in the View Model, where the #Region is, I need to connect to my (preexisting) database, DBUsers

有人可能打破方式做这个,如果我做错了什么?非常感谢你

Can someone break down the way to do this, and if I'm doing anything wrong? Thank you ever so much

推荐答案

如果我正确理解你,EntityFramework应该帮助你使用DB。您需要使用EntityFramework安装NuGet。 (更多信息: https://msdn.microsoft.com/en-us/data /jj574514.aspx http://metanit.com/sharp/entityframework/3.3。 php

If I understand you correctly, the EntityFramework should help you work with the DB. You need to install the NuGet using the EntityFramework. (More info: https://msdn.microsoft.com/en-us/data/jj574514.aspx , http://metanit.com/sharp/entityframework/3.3.php)

app.config

app.config

 <configSections>
    <entityFramework>
       <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
      <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      </providers>
    </entityFramework>
    <connectionStrings>
         <add name="DictionaryPlus" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=DataBaseName;"
    providerName="System.Data.SqlClient"/>
    </connectionStrings>
 </configuration>

xaml.cs档案

public partial class MainWindow : Window
{
    Db_Context db = null;

    public MainWindow()
    { db = new Db_Context(); }

    private async void Window_Loaded(object sender, RoutedEventArgs e)
    {

        await db.Users.LoadAsync();

        /* Data usage */

        /* Binding */

        CollectionViewSource userViewSource = ((CollectionViewSource)(this.FindResource("userViewSource")));
        userViewSource.Source = db.Users.Local;

        /* Add data */

        db.Users.Add(new User());
        await db.SaveChangesAsync();

        this.list_Users.Items.Refresh();

    } 
}

public class Db_Context : DbContext
{
    public Db_Context() : base("DataBaseName") { }
    public DbSet<User> Users { get; set; }
}

xaml档案

 <Window.Resources>
      <CollectionViewSource x:Key="userViewSource" d:DesignSource="{d:DesignInstance {x:Type Model:User}, CreateList=True}"/>
 </Window.Resources>

 <ListBox x:Name="list_Users" DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource userViewSource}}" SelectedValuePath="Id" Focusable="False"/>

这篇关于从正常列表数据转换到wpf xaml中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆