使用Linq将列表框保存到SQL Server数据库

时间:2016-12-30 00:46:02

标签: c# sql-server linq

我正在尝试保存我服务中列表框中显示的项目列表

public class MyItem
{
    public string DisplayVersion { get; set; }
    public string DisplayName { get; set; }
    public string Publisher { get; set;}
}

public List<MyItem> GetListapp()
{
    List<MyItem> items = new List<MyItem>();
    string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";

    using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
    {
        foreach (string sbnm in key.GetSubKeyNames())
        {
            using (RegistryKey subkey = key.OpenSubKey(sbnm))
            {
                string appVersion = (string)subkey.GetValue("DisplayVersion");
                string pub = (string)subkey.GetValue("Publisher");
                String app = Convert.ToString(subkey.GetValue("DisplayName"));

                if (app != "")
                    items.Add(new MyItem() { DisplayName = app, DisplayVersion = appVersion, Publisher = pub });
            }
        }
    }

    return items;
}

我创建了一个包含表ID,DisplayName,DisplayVersion,Publisher

的数据库listapp

我的model.context.cs是:

public partial class listappEntities : DbContext
{
    public listappEntities() : base("name=listappEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<Table> Table { get; set; }
}

我使用ADO.NET实体数据模型连接到数据库,我已经Model.cs这样:

public partial class Table
{
     public int Id { get; set; }
     public string DisplayName { get; set; }
     public string DisplayVersion { get; set; }
     public string Publisher { get; set; }
}

当我点击按钮时,我可以看到来自我服务的listofprograme

private void btnGIA_Click(object sender, RoutedEventArgs e)
{
        MYS.ServiceClient pk = new MYS.ServiceClient();
        listBox.ItemsSource = pk.GetListapp();
}

如何在数据库中保存此列表或更​​新(如果存在)?

请问如何查询我将创建的其他列表框中显示已保存的程序。

0 个答案:

没有答案