Win10之UWP的数据存储

本文介绍在Win10 UWP应用中使用SQLite进行轻量级数据存储的方法。通过示例代码展示了如何创建数据库、插入及查询数据。
原文: Win10之UWP的数据存储

我们知道通常我们开发的时候都要考虑把用户的数据存储到一个数据库里面,而这个数据库则考虑到了整个应用的性能上面,这里我们不考虑SQL server的数据库,我们考虑较为轻量的数据库进行存储。

首先我们新建一个项目,然后把界面用代码处理一下

 <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock Text="把数据存储入数据库"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   FontFamily="楷体"
                   FontSize="24"
                   Foreground="Green"/>
        <TextBlock Text="MySQLite Connection Test"
                   HorizontalAlignment="Center"
                   FontSize="36"
                   FontFamily="Gabriola"
                   Grid.Row="1"
                   Foreground="Green"/>
        <TextBlock Text="用户账号:"
                   Grid.Row="2"/>
        <TextBox x:Name="MyTextBox"
                 Grid.Row="3"
                 PlaceholderText="name"/>
        <TextBlock Text="用户密码:"
                   Grid.Row="4"/>
        <PasswordBox x:Name="MyPassWordBox"
                    Grid.Row="4" 
                     PlaceholderText="password"
                     Margin="0,20,0,0"/>
    <Page.BottomAppBar>
        <CommandBar IsOpen="False"
                    ClosedDisplayMode="Minimal"
                    Background="Green">
            <AppBarButton x:Name="Add" 
                          Label="Add" 
                          Icon="Add"
                          Click="Add_Click"/>
            <AppBarButton x:Name="Show"
                          Label="Show"
                          Icon="Zoom"
                          Click="Show_Click"/>
        </CommandBar>
    </Page.BottomAppBar>

这里写图片描述

然后我们再来处理一下界面后台的事件代码处理

 string path;
        SQLite.Net.SQLiteConnection conn;

这里写图片描述

到这里的时候我们忘记了一件事,没有安装相关的插件,所以再安装下数据库插件
这里写图片描述
还要再安装一个插件
这里写图片描述
紧接着我们安装好了插件后,我再来添加引用,让项目得到插件的支持
这里写图片描述
好了,这次可以好好的写代码了,我在项目中新增了一个类

   public class MyTest
    {
        [PrimaryKey,AutoIncrement]
        public int Id { get; set; }
        public string Name { get; set; }
        public string PassWord { get; set; }
    }

这里写图片描述

我回到我们的主界面的后台写写代码

  path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalCacheFolder.Path, "db.MySQLite");
            conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);
            conn.CreateTable<MyTest>();

这里写图片描述

新增事件的后台的代码处理

private void Add_Click(object sender, RoutedEventArgs e)
        {
            var add = conn.Insert(new MyTest()
            {
                Name = MyTextBox.Text,
                PassWord = MyPassWordBox.Password
            });
            Debug.WriteLine(path);
        }

这里写图片描述

这里的这个方法是在visual studio 2015中显示实时新增的数据

        private void Show_Click(object sender, RoutedEventArgs e)
        {
            var query = conn.Table<MyTest>();
            string result = String.Empty;
            foreach (var item in query)
            {
                result = String.Format("{0}:{1}:{2}", item.Id, item.Name,item.PassWord);
                Debug.WriteLine(result);
            }
        }

这里写图片描述
代码写到这里就已经写完了,我们看看目的达到了没有
这里写图片描述
我们再来看看第二次的效果如何
这里写图片描述

很显然我们写的数据成功的存储到了SQLite的数据库中,所以我们的目的就达到了!!!!

posted on 2017-09-26 10:37 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/7595497.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值