Win10之UWP的数据存储

简介: 原文:Win10之UWP的数据存储 我们知道通常我们开发的时候都要考虑把用户的数据存储到一个数据库里面,而这个数据库则考虑到了整个应用的性能上面,这里我们不考虑SQL server的数据库,我们考虑较为轻量的数据库进行存储。
原文: 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的数据库中,所以我们的目的就达到了!!!!

目录
相关文章
|
Windows
Qt-Windows下发布应用程序
QtWindows下发布应用程序
79 0
|
Windows
windows下动态发布QT程序
windows下动态发布QT程序
147 0
|
缓存 C# 开发工具
将 WPF、UWP 以及其他各种类型的旧 csproj 迁移成基于 Microsoft.NET.Sdk 的新 csproj
原文 将 WPF、UWP 以及其他各种类型的旧 csproj 迁移成基于 Microsoft.NET.Sdk 的新 csproj 写过 .NET Standard 类库或者 .NET Core 程序的你一定非常喜欢微软为他们新开发的项目文件(对于 C#,则是 csproj 文件)。
1190 0
|
C# 开发工具
[UWP]理解及扩展Expander
原文:[UWP]理解及扩展Expander 1. 前言 最近在自定义Expander的样式,顺便看了看它的源码。 Expander控件是一个ContentControl,它通过IsExpanded属性或者通过点击Header中的ToggleButton控制内容展开或隐藏。
864 0
|
C# Windows
UWP使用AppService向另一个UWP客户端应用程序提供服务
原文:UWP使用AppService向另一个UWP客户端应用程序提供服务 在上篇里,我使用的是寄宿在WPF上的WCF进行两个程序间的通信,在解决问题的同时,我的同事也在思考能否使用UWP来做这件事。于是,我们发现了App Service,两个UWP应用沟通的桥梁。
1183 0