2013-08-27 63 views
0

我是C#的初学者程序员,所以我认为我的问题的解决方案可能很简单,但经过几天的寻找之后,我还没有找到任何对我有用的东西。WP7后台代理数据库访问

我有一个WP7应用程序,其中包含使用SQL CE创建的数据库。

CLASS PorniBD.cs

using System; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Data.Linq.Mapping; 

namespace PhoneClassLibrary1 
{ 
    [Table(Name = "Papilleros")] 
    public class PorniBD 
    { 
     [Column(IsPrimaryKey = true, IsDbGenerated = true)] 
     public int Id { get; set; } 
     [Column(CanBeNull = false)] 
     public String Nombre { get; set; } 
     [Column(CanBeNull = false)] 
     public String FechaNac { get; set; } 
     [Column(CanBeNull = false)] 
     public Boolean Activo { get; set; } 
     [Column(CanBeNull = false)] 
     public String Icono { get; set; } 
    } 
} 

CLASS PorniContext.cs

using System; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Data.Linq.Mapping; 
using System.Data.Linq; 

namespace PhoneClassLibrary1 
{ 
    public class PorniContext : DataContext 
    { 
     public PorniContext(string connectionString) : base(connectionString) 
     { 
      // 
     } 

     public Table<PorniBD> Papilleros 
     { 
      get 
      { 
       return this.GetTable<PorniBD>(); 
      } 
     } 
    } 
} 

我的应用程序在另一个项目中创建就像我在本页面已经学到了后台代理:Link

现在,我需要从后台代理读取应用程序数据库,并且此类包含以下OnInvoke void:

protected override void OnInvoke(ScheduledTask task) 
     { 
      List<PorniBD> listapapilleros = new List<PorniBD>(); 
      using (PorniContext basedatos = new PorniContext("Data Source='isostore:/basedatos.sdf'")) 
      { 
       listapornis = basedatos.Papilleros.ToList(); 
      } 

      // Launch a toast to show that the agent is running. 
      // The toast will not be shown if the foreground application is running. 

      ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(10)); 
      NotifyComplete(); 
     } 

但它是不可能的,因为数据源中分离是在每一个项目(我认为)不同,我想,这是必要的修复更多的东西......

非常感谢你们的帮助,对不起,如果我的英语级别是使我的解释有点难以理解...

回答

1

只需创建一个类型为“Windows Phone类库”的第三个项目。将数据库代码移到第三个项目中,然后从主项目和后台代理项目中引用它。

+0

感谢您的回答KooKiz。但是,当我创建第三个项目时,在使用OnInvoke行void时出现问题。这个错误与DataContext的ToList()函数有关:不可能调用它(我不知道为什么,因为当我从应用程序项目使用toList()时,调用正确地工作)。 (请参阅原始消息中更新的代码) – user2723183

+0

@ user2723183确保您的文件上方有'using System.Linq'行 –