打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Windows8SQLite数据库操作[Async]

Windows8SQLite数据库操作[Async]

(2013-01-22 13:33:52)
标签:

杂谈

    public sealed partialclass MainPage : Page
    {
       privateList<Persons> personsTableList;
       privateList<Orders> ordersTableList;

       private SQLiteAsyncConnectionasyncDataBase;

       public MainPage()
       {
          this.InitializeComponent();

          ConnData();

          InitData();

       }

       private void ConnData()
       {
           var dbPath=Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,"db.sqlite");
          asyncDataBase = new SQLite.SQLiteAsyncConnection(dbPath);
       }

       private async void InitData()
       {
           var result= await asyncDataBase.CreateTablesAsync<Persons,Orders>();

          List<Persons> persons = newList<Persons>()
           {
              newPersons(){FirstName="Adams",LastName="John",Address="OxfordStreet",City="London"},
              newPersons(){FirstName="Bush",LastName="George",Address="FifthAvenue",City="New York"},
              newPersons(){FirstName="Carter",LastName="Thomas",Address="ChanganStreet",City="Beijing"}
           };

          List<Orders> orders = newList<Orders>()
           {
              newOrders(){OrderNo="77895",ID_P=3},
              newOrders(){OrderNo="44678",ID_P=3},
              newOrders(){OrderNo="22456",ID_P=1},
              newOrders(){OrderNo="24562",ID_P=1},
              newOrders(){OrderNo="34764",ID_P=65}
           };

           awaitasyncDataBase.InsertAllAsync(persons);
           awaitasyncDataBase.InsertAllAsync(orders);

           varpersonsTable =asyncDataBase.Table<Persons>();
          personsTableList = await personsTable.ToListAsync();
          this.lv1.ItemsSource = personsTableList;

           varordersTable =asyncDataBase.Table<Orders>();
          ordersTableList = await ordersTable.ToListAsync();
          this.lv2.ItemsSource = ordersTableList;
       }

       private async void InsertDataBase_Click(objectsender, RoutedEventArgs e)
       {
           awaitasyncDataBase.InsertAsync(new Persons() { FirstName = "Aaron",LastName = "Xue", Address = "MinHang", City = "Shanghai" });

           varpersionTable =asyncDataBase.Table<Persons>();
          this.lv3.ItemsSource = await persionTable.ToListAsync();
       }

       private async void ModifyDataBase_Click(objectsender, RoutedEventArgs e)
       {
           stringcity = "Shanghai";
           int i =await asyncDataBase.ExecuteAsync("UPDATE Persons SET City=? WHEREFirstName='Bush'", city);

           varpersonsTable =asyncDataBase.Table<Persons>();
          this.lv3.ItemsSource = await personsTable.ToListAsync();
       }

       private async void QueryDataBase_Click(objectsender, RoutedEventArgs e)
       {
          List<Result> re = awaitasyncDataBase.QueryAsync<Result>("SELECTPersons.FirstName,Persons.LastName,Orders.OrderNo FROM Persons LEFTJOIN Orders ON Persons.ID_P=Orders.ID_P ORDER BYPersons.FirstName");
          this.lv4.ItemsSource = re;
       }

    }


    public classPersons
    {
       [SQLite.AutoIncrement, SQLite.PrimaryKey]
       public int ID_P { get; set; }
       public string FirstName { get; set; }
       public string LastName { get; set; }
       public string Address { get; set; }
       public string City { get; set; }
    }

    public classOrders
    {
       [SQLite.AutoIncrement, SQLite.PrimaryKey]
       public int ID_O { get; set; }
       public string OrderNo { get; set; }
       public int ID_P { get; set; }
    }

    public classResult
    {
       public string FirstName { get; set; }
       public string LastName { get; set; }
       public string OrderNo { get; set; }
    }

    <GridBackground="{StaticResourceApplicationPageBackgroundThemeBrush}">
      <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
          <RowDefinition Height="*" />
          <RowDefinition Height="100" />
          <RowDefinition Height="400" />
      </Grid.RowDefinitions>
       <ListView Name="lv1" Grid.Row="0"Grid.Column="0">
          <ListView.ItemTemplate>
             <DataTemplate>
                 <StackPanelOrientation="Horizontal">
                    <StackPanel.Resources>
                        <StyleTargetType="TextBlock">
                           <Setter Property="Width"Value="150" />
                           <Setter Property="TextAlignment"Value="Left" />
                       </Style>
                    </StackPanel.Resources>
                    <TextBlock Width="50" Text="{Binding ID_P}"/>
                    <TextBlock Text="{Binding FirstName}"/>
                    <TextBlock Text="{Binding LastName}"/>
                    <TextBlock Text="{Binding Address}"/>
                    <TextBlock Text="{Binding City}"/>
                </StackPanel>
             </DataTemplate>
          </ListView.ItemTemplate>
       </ListView>
       <ListView Name="lv2" Grid.Row="0"Grid.Column="1">
          <ListView.ItemTemplate>
             <DataTemplate>
                 <StackPanelOrientation="Horizontal">
                    <StackPanel.Resources>
                        <StyleTargetType="TextBlock">
                           <Setter Property="Width"Value="150" />
                           <Setter Property="TextAlignment"Value="Left" />
                       </Style>
                    </StackPanel.Resources>
                    <TextBlock Width="50" Text="{Binding ID_O}"/>
                    <TextBlock Text="{Binding OrderNo}"/>
                    <TextBlock Text="{Binding ID_P}"/>
                </StackPanel>
             </DataTemplate>
          </ListView.ItemTemplate>
       </ListView>
       <StackPanel Grid.Row="1"Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal"HorizontalAlignment="Left">
          <Button Content="Add" Click="InsertDataBase_Click"/>
          <Button Content="Update"Click="ModifyDataBase_Click" />
          <Button Content="Query" Click="QueryDataBase_Click"/>
      </StackPanel>
       <ListView Grid.Row="2"Grid.Column="0" Name="lv3">
          <ListView.ItemTemplate>
             <DataTemplate>
                 <StackPanelOrientation="Horizontal">
                    <StackPanel.Resources>
                        <StyleTargetType="TextBlock">
                           <Setter Property="Width"Value="150" />
                           <Setter Property="TextAlignment"Value="Left" />
                       </Style>
                    </StackPanel.Resources>
                    <TextBlock Width="50" Text="{Binding ID_P}"/>
                    <TextBlock Text="{Binding FirstName}"/>
                    <TextBlock Text="{Binding LastName}"/>
                    <TextBlock Text="{Binding Address}"/>
                    <TextBlock Text="{Binding City}"/>
                </StackPanel>
             </DataTemplate>
          </ListView.ItemTemplate>
       </ListView>
       <ListView Grid.Row="2"Grid.Column="1" Name="lv4">
          <ListView.ItemTemplate>
             <DataTemplate>
                 <StackPanelOrientation="Horizontal">
                    <StackPanel.Resources>
                        <StyleTargetType="TextBlock">
                           <Setter Property="Width"Value="150" />
                           <Setter Property="TextAlignment"Value="Left" />
                       </Style>
                    </StackPanel.Resources>
                    <TextBlock Text="{Binding FirstName}"/>
                    <TextBlock Text="{Binding LastName}"/>
                    <TextBlock Text="{Binding OrderNo}"/>
                </StackPanel>
             </DataTemplate>
          </ListView.ItemTemplate>
       </ListView>
   </Grid>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
无废话WPF系列9: Binding的源
ListView(一)
WPF中的控件布局
Windows Presentation Foundation 数据绑定:第一部分
SQL JOIN
sql常用语句整理(包括增删改查)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服