souphia nisus
souphia nisus

Reputation: 13

data mode : read write with c# local database in wp7

I created a local db with helper app project. and deployed it from isolate storage to installation folder,i added to project directory with content build action by add existing item. my problem is that i want to insert data, but i don't know how to move the db file to isolate storage to insert and data must add to my .sdf file that is locate in my project directory also.

Upvotes: 0

Views: 886

Answers (1)

Alex
Alex

Reputation: 1084

Souphia, While learning to use WP, I wrote a simple application that tracked tasks. One version of that app stored all task data in Sql on the phone. You can read the post and download all the code for the app here:

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

But, here is some of the code from that project:

First we have the model class decorated with the appropriate attributes:

[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));
    }
}

In the constructor in app.xaml.cs, I have the following:

 TaskMasterDataContext = new  TaskMasterDataContext();

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

and here is the TaskMasterDataContext.cs code

 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";
        }
    }

The DatabaseHelper class is just there to populate the DB with some test data after its created. I hope this helps.

Upvotes: 1

Related Questions