我现在正在创建一个应用程序,允许用户从本地数据库中创建和检索数据。有一个名为LIKE的列,默认值为int 0。我有一个按钮,但是我是否可以按下这个按钮,那么int 0的默认值将变成1?每次按下它,它都会将一个添加到默认值int中。我怎么能这么做?
发布于 2014-01-15 16:16:03
假设您的数据库上下文设置为我在注释中提到的教程,则按钮单击事件可以如下所示:
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 设置。
https://stackoverflow.com/questions/21137275
复制相似问题