sqlite with uwp

13
SQLite with UWP Prepared by ENG SOON CHEAH Microsoft MVP

Upload: cheah-eng-soon

Post on 16-Apr-2017

2.297 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: SQLite with UWP

SQLite with UWPPrepared by

ENG SOON CHEAHMicrosoft MVP

Page 2: SQLite with UWP

Install SQLite-UAP extensions form NuGet Package Manager

Page 3: SQLite with UWP

Next Install SQLite.Net-PCL extension from NuGet Package

Page 4: SQLite with UWP

Design UI• Include 4 functions CRUD Operations• Bind data to a ListBox

Page 5: SQLite with UWP

Design UI<Grid Background="#FFF589E2">      <Grid.ColumnDefinitions>          <ColumnDefinition></ColumnDefinition>      </Grid.ColumnDefinitions>      <Grid.RowDefinitions>          <RowDefinition Height="Auto"></RowDefinition>          <RowDefinition Height="Auto"></RowDefinition>          <RowDefinition Height="Auto"></RowDefinition>          <RowDefinition Height="Auto"></RowDefinition>          <RowDefinition Height="*"></RowDefinition>      </Grid.RowDefinitions>      <Button x:Name="CreateDBbutton" Grid.Row="0" Content="Create Local Database" HorizontalAlignment="Center" VerticalAlignment="Top" Click="button_Click" />      <Button x:Name="create" Grid.Row="1" Content="Create New Students" HorizontalAlignment="Center" Click="create_Click"></Button>      <Button x:Name="read" Grid.Row="2" Content="Read Students List" Width="300" Click="read_Click" HorizontalAlignment="Center"></Button>      <Button x:Name="update" Grid.Row="3" Content="Update Details" Width="300" Click="update_Click" HorizontalAlignment="Stretch"></Button>      <ListView x:Name="allstudents" HorizontalAlignment="Stretch" Grid.Row="4">          <ListView.ItemTemplate>              <DataTemplate>                  <TextBlock x:Name="ee" Text="{Binding Name}" FontSize="14"></TextBlock>              </DataTemplate>          </ListView.ItemTemplate>      </ListView>  </Grid>  

Page 6: SQLite with UWP

Create a Classpublic class Students  {      [SQLite.Net.Attributes.PrimaryKey, SQLite.Net.Attributes.AutoIncrement]      public int Id      {          get;          set;      }      public string Name      {          get;          set;      }      public string Address      {          get;          set;      }      public string Mobile      {          get;          set;      }      public Students()      {}      public Students(string name, string address, string mobile)      {          Name = name;          Address = address;          Mobile = mobile;      }  }  

Page 7: SQLite with UWP

Create DBpublic static void CreateDatabase()  {      var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Studentdb.sqlite");      using(SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sqlpath))      {          conn.CreateTable < Students > ();      }  }  

Page 8: SQLite with UWP

Insert Detailspublic void Insert(Students objContact)  {      var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Studentdb.sqlite");      using(SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sqlpath))      {          conn.RunInTransaction(() =>          {              conn.Insert(objContact);          });      }  }  

Page 9: SQLite with UWP

Retrieve Details// Retrieve the specific contact from the database.  public Students ReadContact(int contactid)  {      var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Studentdb.sqlite");      using(SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sqlpath))      {          var existingconact = conn.Query < Students > ("select * from Students where Id =" + contactid).FirstOrDefault();          return existingconact;      }  }  

Page 10: SQLite with UWP

Read Details//Read All Student details  public ObservableCollection < Students > ReadAllStudents()  {      var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Studentdb.sqlite");      using(SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sqlpath))      {          List < Students > myCollection = conn.Table < Students > ().ToList < Students > ();          ObservableCollection < Students > StudentsList = new ObservableCollection < Students > (myCollection);          return StudentsList;      }  }  

Page 11: SQLite with UWP

Update Details//Update student detaisl  public void UpdateDetails(string name)  {      var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Studentdb.sqlite");      using(SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sqlpath))      {          var existingconact = conn.Query < Students > ("select * from Students where Name =" + name).FirstOrDefault();          if (existingconact != null)          {              existingconact.Name = name;              existingconact.Address = "NewAddress";              existingconact.Mobile = "962623233";              conn.RunInTransaction(() =>              {                  conn.Update(existingconact);              });          }      }  }  

Page 12: SQLite with UWP

Delete Details//Delete all student or delete student table  public void DeleteAllContact()  {      var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Studentdb.sqlite");      using(SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sqlpath))      {          conn.DropTable < Students > ();          conn.CreateTable < Students > ();          conn.Dispose();          conn.Close();      }  }  Delete specific student  //Delete specific student  public void DeleteContact(int Id)  {      var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Studentdb.sqlite");      using(SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sqlpath))      {          var existingconact = conn.Query < Students > ("select * from Studentdb where Id =" + Id).FirstOrDefault();          if (existingconact != null)          {              conn.RunInTransaction(() =>              {                  conn.Delete(existingconact);              });          }      }  }