2013-01-16 47 views
1

我使用本地数据库将数据存储在Windows Phone 8应用程序中。起初我有存储在JSON对象中的数据,它被转换为我的类对象,然后收集我尝试存储在本地数据库中的这些对象。我正在检查调试模式,数据在这些对象中,但是当我检查数据库时,它是空的。填充后本地数据库为空

这是我如何将数据从收集到数据库:

// Data context for the local database 
private TablesDataContext tablesDB; 

// Define the query to gather all of items. 
var customersTablesInDB = from CustomerItem todo in tablesDB.CustomersTable 
           select todo; 

// Execute the query and place the results into a collection. 
CustomersTable = new ObservableCollection<CustomerItem>(customersTablesInDB); 

foreach (Customer customer in customersList) 
{ 
    // Create a new item 
    CustomerItem newCustomer = new CustomerItem 
    { 
     Id = customer.id, 
     Number = customer.number.Value, 
     Name = customer.name, 
     Email = customer.email 
    }; 

    // Add item to the observable collection. 
    CustomersTable.Add(newCustomer); 

    // Add item to the local database. 
    tablesDB.CustomersTable.InsertOnSubmit(newCustomer); 
} 

这里是我的DataContext类:

public class TablesDataContext : DataContext 
{ 
    // Specify the connection string as a static, used in main page and app.xaml. 
    public static string DBConnectionString = "Data Source=isostore:/Customers.sdf"; 

    // Pass the connection string to the base class. 
    public TablesDataContext(string connectionString) 
     : base(connectionString) 
    { } 

    // Specify a single table for the items. 
    public Table<CustomerItem> CustomersTable; 
} 

这里是我的CustomerItem类:

[Table] 
public class CustomerItem : INotifyPropertyChanged, INotifyPropertyChanging 
{ 
    // Define ID: private field, public property and database column. 
    private int _id; 

    [Column(IsPrimaryKey = true, IsDbGenerated = false, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)] 
    public int Id 
    { 
     get 
     { 
      return _id; 
     } 
     set 
     { 
      if (_id != value) 
      { 
       NotifyPropertyChanging("Id"); 
       _id = value; 
       NotifyPropertyChanged("Id"); 
      } 
     } 
    } 

    // Define item name: private field, public property and database column. 
    private int? _number; 

    [Column] 
    public int? Number 
    { 
     get 
     { 
      return _number; 
     } 
     set 
     { 
      if (_number != value) 
      { 
       NotifyPropertyChanging("Number"); 
       _number = value; 
       NotifyPropertyChanged("Number"); 
      } 
     } 
    } 

    // Define completion value: private field, public property and database column. 
    private String _name; 

    [Column] 
    public String Name 
    { 
     get 
     { 
      return _name; 
     } 
     set 
     { 
      if (_name != value) 
      { 
       NotifyPropertyChanging("Name"); 
       name = value; 
       NotifyPropertyChanged("Name"); 
      } 
     } 
    } 

    // Define completion value: private field, public property and database column. 
    private String _email; 

    [Column] 
    public String Email 
    { 
     get 
     { 
      return _email; 
     } 
     set 
     { 
      if (_email != value) 
      { 
       NotifyPropertyChanging("Email"); 
       _email = value; 
       NotifyPropertyChanged("Email"); 
      } 
     } 
    } 


    // Version column aids update performance. 
    [Column(IsVersion = true)] 
    private Binary _version; 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    // Used to notify the page that a data context property changed 
    private void NotifyPropertyChanged(String propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    #endregion 

    #region INotifyPropertyChanging Members 

    public event PropertyChangingEventHandler PropertyChanging; 

    // Used to notify the data context that a data context property is about to change 
    private void NotifyPropertyChanging(String propertyName) 
    { 
     if (PropertyChanging != null) 
     { 
      PropertyChanging(this, new PropertyChangingEventArgs(propertyName)); 
     } 
    } 

    #endregion 

} 
+2

是不是缺少一个“tablesDB.SubmitChanges();” ? – robertk

+0

您从不运行“插入”查询。所有'InsertOnSubmit'确实是“将挂起插入状态的实体添加到此表”。这意味着更改只是未决的。当您的应用程序关闭时,挂起的更改可能会被丢弃。 –

+0

@robertftw是的,我添加了该行,并且一切正常。谢谢:)。把它写成答案,我会接受它。 – dziwna

回答

1

您在foreach循环后缺少“tablesDB.SubmitChanges()”。