2013-01-16 66 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()」。