我正在使用本地数据库将我的数据存储在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
}
答案 0 :(得分:1)
你的foreach循环后缺少“tablesDB.SubmitChanges()”。