Win10之UWP的数据存储

来源:互联网 发布:淘宝偷换宝贝怎么举报 编辑:程序博客网 时间:2024/06/04 18:27

我们知道通常我们开发的时候都要考虑把用户的数据存储到一个数据库里面,而这个数据库则考虑到了整个应用的性能上面,这里我们不考虑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的数据库中,所以我们的目的就达到了!!!!

2 0