SubmitChanges()后未保存本地数据库更改

时间:2013-05-14 08:10:32

标签: sql database linq-to-sql windows-phone-8 isolatedstorage

我正在使用本地数据库来存储我的数据。我有*.sdf加载的文件Isolated Storage,因为我希望我的应用程序也能在离线模式下工作,所以如果用户不想要,他就不必更新数据。

这就是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:/MyDatabase.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;
    public Table<ProductItem> ProductsTable;
}

如果需要,我在App.xaml.cs中创建我的数据库:

// Create the database if it does not exist.
using (TablesDataContext db = new TablesDataContext(TablesDataContext.DBConnectionString))
{
    if (db.DatabaseExists() == false)
    {
        //Create the database
        db.CreateDatabase();
    }
}
dataContext = new UserDataContext();

我更新所需信息的SettingsPage

public partial class SettingsPage : PhoneApplicationPage
{

    // Data context for the local database
    private TablesDataContext tablesDB;

    // Define an observable collection property that controls can bind to.
    private ObservableCollection<CustomerItem> _customersTable;
    public ObservableCollection<CustomerItem> CustomersTable
    {
        get
        {
            return _customersTable;
        }
        set
        {
            if (_customersTable != value)
            {
                _customersTable = value;
                NotifyPropertyChanged("CustomersTable");
            }
        }
    }

    // Define an observable collection property that controls can bind to.
    private ObservableCollection<ProductItem> _productTable;
    public ObservableCollection<ProductItem> ProductTable
    {
        get
        {
            return _productTable;
        }
        set
        {
            if (_productTable != value)
            {
                _productTable = value;
                NotifyPropertyChanged("ProductTable");
            }
        }
    }

    public void addingCustomersToDatabase(List<CustomerJSON> customersList)
    {

        // Define the query to gather all of the to-do 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);

        tablesDB.CustomersTable.DeleteAllOnSubmit(CustomersTable);
        tablesDB.SubmitChanges();

        foreach (CustomerJSON customer in customersList)
        {
            // Create a new to-do item based on the text box.
            CustomerItem newCustomer = new CustomerItem
            {
                // Filling customer data from JSON object
            };

            // Add a to-do item to the observable collection.
            CustomersTable.Add(newCustomer);

            // Add a to-do item to the local database.
            tablesDB.CustomersTable.InsertOnSubmit(newCustomer);
            tablesDB.SubmitChanges();
        }
    }

    public void addingProductsToDatabase(List<ProductJSON> ProductsList)
    {
           // Same things as in previouse method but with products objects
    }

    private void Load_Click(object sender, RoutedEventArgs e)
    {
        addingCustomersToDatabase(customersList);
        addingProductsToDatabase(productsList);
    }
}

更新信息后,我确信数据是最新的,但直到我关闭我的应用程序。如果我重新打开应用程序,它仍然有旧数据。如果我理解正确,不应该将其保存在我的*.sdf文件中吗?顺便说一下,我正在使用Linq。

编辑:

我设法检查它,我发现在JSON解析之后,数据库被更新(* .sdf文件填充了新数据),但是当我关闭并再次打开应用程序时(不构建,只需重新打开)模拟器),它又有旧数据。

1 个答案:

答案 0 :(得分:0)

请尝试以下

  1. 验证JSON数据是否已更新
  2. 尝试使用“AddObject”而不是“InsertOnSubmit”,如下所示

    tablesDB.CustomersTable.AddObject(newCustomer);