2013-01-16 2 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

'Insert' 쿼리를 실행하지 않습니다. 'InsertOnSubmit'은 "이 테이블에 보류중인 삽입 상태의 엔티티를 추가합니다. "입니다. 이는 변경 사항이 보류 중임을 의미합니다. 응용 프로그램이 닫히면 보류중인 변경 사항이 무시됩니다. –

+0

@robertftw 네, 그 라인을 추가했는데 모든 것이 잘 작동합니다. 고맙습니다:). 대답으로 쓰면 받아 들일 것입니다. – dziwna

답변

1

내가 컬렉션에서 데이터베이스에 데이터를 이동하는 방법입니다 foreach-loop 다음에 "tablesDB.SubmitChanges()"가 누락되었습니다.

관련 문제