C# Windows phone 7数据库添加值
我现在正在创建一个应用程序,它允许用户从本地数据库创建和检索数据。有一列名为LIKE,默认值为int 0。我有一个按钮,但我是否可以按下该按钮,然后int 0的默认值将变为1?每次我按下它都会将一个添加到默认值int中。如何才能做到这一点?假设您的数据库上下文设置为我在注释中提到的,则您的按钮单击事件可能如下所示:C# Windows phone 7数据库添加值,c#,database,windows-phone-7,C#,Database,Windows Phone 7,我现在正在创建一个应用程序,它允许用户从本地数据库创建和检索数据。有一列名为LIKE,默认值为int 0。我有一个按钮,但我是否可以按下该按钮,然后int 0的默认值将变为1?每次我按下它都会将一个添加到默认值int中。如何才能做到这一点?假设您的数据库上下文设置为我在注释中提到的,则您的按钮单击事件可能如下所示: private void IncrementLikes_Click(object sender, RoutedEventArgs e) { //whe
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);
}
//您的数据库上下文
私有数据库DataContext,如b;
//定义控件可以绑定到的可观察集合属性。
私人可观察收集;
公开收集
{
得到
{
回报你喜欢的东西;
}
设置
{
如果(_likes!=值)
{
_喜欢=价值;
NotifyPropertyChanged(“喜欢”);
}
}
}
//建造师
公共主页()
{
初始化组件();
LikeDB=newyourdatabasedatacontext(YourDatabaseDataContext.DBConnectionString);
this.DataContext=this;
}
受保护的覆盖无效OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
//定义查询以收集所有待办事项。
var likesFromDB=from LikeItem like in LikeDB.Likes
选择喜欢的;
//执行查询并将结果放入集合中。
Likes=新的可观察集合(likesFromDB);
//调用基方法。
基地。导航到(e);
}
仅为一个值使用数据库可能太多,因此您可能希望查看其他替代方法,例如Windows Phone。我建议您阅读本教程:@Eugene这不是我需要的,我需要将value int 1自动添加到LIKE列。您需要检索用于存储值的任何类,将其递增1,然后将更改提交回数据库。希望有一些示例可以参考=),我将对此进行研究,非常感谢!