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
}