2014-01-15 123 views
0

我現在正在創建一個應用程序,它允許用戶從本地數據庫創建和檢索數據。有一列名爲LIKE,默認值爲int 0。我有一個按鈕,但無論如何,我可以按下該按鈕,然後默認值int 0將變爲1?每次我按它都會將默認值int加1。我怎樣才能做到這一點 ?Windows phone 7數據庫添加值

+0

我建議你閱讀本教程:http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202876(v=vs.105).aspx – Eugene

+0

@Eugene這不是我所需要的,我需要在LIKE列中自動添加值爲int 1的值。 – NoobieNeedHelp

+0

您需要檢索用於存儲值的任何類,將其增加1,然後將更改提交回數據庫。 – Eugene

回答

1

假設你的數據庫上下文設置爲在tutorial我在評論中提到,你的按鈕click事件如下所示:

private void IncrementLikes_Click(object sender, RoutedEventArgs e) 
    { 
     //where Likes is your ObservableCollection keeping data from database 
     LikeItem oneAndOnly = Likes.First() as LikeItem; 
     //you pulled one and only LikeItem object, now you increment the likes count 
     oneAndOnly.LikesCount += 1; 
     //and here you tell the database that the changes need to be saved. 
     //This call can be delayed to the OnNavigatedFrom event, 
     // depending on your needs. 
     LikeDB.SubmitChanges(); 
    } 

,這裏是一些支持的代碼屬於cs文件

//your database context 
    private YourDatabaseDataContext LikeDB; 

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

    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 
     LikeDB = new YourDatabaseDataContext(YourDatabaseDataContext.DBConnectionString); 
     this.DataContext = this; 
    } 

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     // Define the query to gather all of the to-do items. 
     var likesFromDB = from LikeItem like in LikeDB.Likes 
          select like; 

     // Execute the query and place the results into a collection. 
     Likes = new ObservableCollection<LikeItem>(likesFromDB); 

     // Call the base method. 
     base.OnNavigatedTo(e); 
    } 

使用數據庫只是一個值可能是太多,所以您可能想看看其他的辦法,比如Windows Phone的settings:頁面。

+0

將研究它,非常感謝! – NoobieNeedHelp

+0

很高興我可以幫助:) – Eugene