本地数据库填满后清空

本文关键字:清空 数据库 | 更新日期: 2023-09-27 17:53:30

我使用本地数据库在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
}

本地数据库填满后清空

在foreach-loop之后缺少"tablesDB.SubmitChanges()"