首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows 7数据库添加值

Windows 7数据库添加值
EN

Stack Overflow用户
提问于 2014-01-15 12:20:02
回答 1查看 63关注 0票数 0

我现在正在创建一个应用程序,允许用户从本地数据库中创建和检索数据。有一个名为LIKE的列,默认值为int 0。我有一个按钮,但是我是否可以按下这个按钮,那么int 0的默认值将变成1?每次按下它,它都会将一个添加到默认值int中。我怎么能这么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-15 16:16:03

假设您的数据库上下文设置为我在注释中提到的教程,则按钮单击事件可以如下所示:

代码语言:javascript
复制
    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文件的一些支持代码:

代码语言:javascript
复制
    //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 设置

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21137275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档