数据模式:用wp7中的c#本地数据库读写

时间:2012-03-24 13:26:53

标签: windows-phone-7 windows-phone-7.1

我使用帮助应用程序项目创建了一个本地数据库。并将它从隔离存储部署到安装文件夹,我通过添加现有项目添加到具有内容构建操作的项目目录。我的问题是我想插入数据,但我不知道如何移动db文件以隔离存储插入,数据必须添加到我的项目目录中的.sdf文件中。

1 个答案:

答案 0 :(得分:1)

Souphia, 在学习使用WP时,我编写了一个跟踪任务的简单应用程序。 该应用程序的一个版本将所有任务数据存储在手机上的Sql中。 您可以在此处阅读帖子并下载该应用的所有代码:

http://www.ritzcovan.com/2012/02/building-a-simple-windows-phone-app-part-3/

但是,这是该项目的一些代码:

首先,我们使用适当的属性修饰模型类:

[Table]
public class Task : INotifyPropertyChanged, INotifyPropertyChanging
{
    [Column(IsDbGenerated = false, IsPrimaryKey = true, CanBeNull = false)]
    public string Id
    {
        get { return _id; }
        set
        {
            NotifyPropertyChanging("Id");
            _id = value;
            NotifyPropertyChanging("Id");
        }
    }

    [Column]
    public string Name
    {
        get { return _name; }
        set
        {
            NotifyPropertyChanging("Name");
            _name = value;
            NotifyPropertyChanged("Name");
        }
    }

    [Column]
    public string Category
    {
        get { return _category; }
        set
        {
            NotifyPropertyChanging("Category");
            _category = value;
            NotifyPropertyChanged("Category");
        }
    }

    [Column]
    public DateTime? DueDate
    {
        get { return _dueDate; }
        set
        {
            NotifyPropertyChanging("DueDate");
            _dueDate = value;
            NotifyPropertyChanged("DueDate");
        }
    }

    [Column]
    public DateTime? CreateDate
    {
        get { return _createDate; }
        set
        {
            NotifyPropertyChanging("CreateDate");
            _createDate = value;
            NotifyPropertyChanged("CreateDate");
        }
    }

    [Column]
    public bool IsComplete
    {
        get { return _isComplete; }
        set
        {
            NotifyPropertyChanging("IsComplete");
            _isComplete = value;
            NotifyPropertyChanged("IsComplete");
        }
    }

    [Column(IsVersion = true)] private Binary _version;

    private string _id;
    private bool _isComplete;
    private DateTime? _createDate;
    private DateTime? _dueDate;
    private string _name;
    private string _category;
    public event PropertyChangedEventHandler PropertyChanged;
    public event PropertyChangingEventHandler PropertyChanging;

    public void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

    public void NotifyPropertyChanging(string property)
    {
        if (PropertyChanging != null)
            PropertyChanging(this, new PropertyChangingEventArgs(property));
    }
}

在app.xaml.cs的构造函数中,我有以下内容:

 TaskMasterDataContext = new  TaskMasterDataContext();

 if (!TaskMasterDataContext.DatabaseExists())
 {
    TaskMasterDataContext.CreateDatabase();
    DatabaseHelper.SetupDatabase(TaskMasterDataContext);
 }

这里是TaskMasterDataContext.cs代码

 public class TaskMasterDataContext : DataContext
    {
        public TaskMasterDataContext() : base("Data Source=isostore:/TaskMasterData.sdf")
        {
        }
        public Table<Task> Tasks;
    }


    public static class DatabaseHelper
    {
        public static void SetupDatabase(TaskMasterDataContext dataContext)
        {
            string category = string.Empty;
            var tasks = new List<Task>();
            for (int i = 0; i < 20; i++)
            {
                tasks.Add(new Task()
                              {
                                  Id = System.Guid.NewGuid().ToString(),
                                  Category = GetCategoryString(i),
                                  CreateDate = DateTime.Now,
                                  DueDate = DateTime.Now.AddDays(new Random().Next(1, 30)),
                                  IsComplete = false,
                                  Name = String.Format("{0} Task # {1}", GetCategoryString(i), i)
                              });
            }
            dataContext.Tasks.InsertAllOnSubmit(tasks);
            dataContext.SubmitChanges();
        }

        private static string GetCategoryString(int i)
        {
            if (i%2 == 0)
                return "home";

            if (i%3 == 0)
                return "personal";

            return "work";
        }
    }

DatabaseHelper类就是在创建DB之后用DB填充一些测试数据。 我希望这会有所帮助。