Here are the steps,

Step 1

Firstly, Create a Blank Windows Project.



Step 2

Install ‘sqlite-net’ from the Package manager and also Add reference for ‘SQLite for Windows Runtime’.

In the MainPage.xaml, we add the following buttons with click events-

Complete XAML code

  1. <Page
  2. x:Class="SQLiteMerge.MainPage"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:SQLiteMerge"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9. <Grid Background="#FF5A7FD6">
  10. <Button x:Name="btnCreateFirstDb" Content="Create First Database" HorizontalAlignment="Left" Margin="154,97,0,0" VerticalAlignment="Top" Width="440" Height="77" Click="btnCreateFirstDb_Click"/>
  11. <Button x:Name="btnCreateSecondDb" Content="Create Second Database" HorizontalAlignment="Left" Margin="148,206,0,0" VerticalAlignment="Top" Width="446" Height="77" Click="btnCreateSecondDb_Click"/>
  12. <Button x:Name="btnMerge" Content="Merge Two Database" HorizontalAlignment="Left" Margin="154,316,0,0" VerticalAlignment="Top" Width="446" Height="77" Click="btnMerge_Click"/>
  13. </Grid>
  14. </Page>
Step 3

In the code behind MainPage.xaml.cs, for each first two buttons we add the code for creating two Databases and a third button to merge values from second database to the first one.

Complete Code snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices.WindowsRuntime;
  6. using Windows.Foundation;
  7. using Windows.Foundation.Collections;
  8. using Windows.UI.Xaml;
  9. using Windows.UI.Xaml.Controls;
  10. using Windows.UI.Xaml.Controls.Primitives;
  11. using Windows.UI.Xaml.Data;
  12. using Windows.UI.Xaml.Input;
  13. using Windows.UI.Xaml.Media;
  14. using Windows.UI.Xaml.Navigation;
  15. using SQLite;
  16. using Windows.Storage;
  17. using System.Diagnostics;
  18. namespace SQLiteMerge
  19. {
  20. public sealed partial class MainPage : Page
  21. {
  22. private SQLiteAsyncConnection dbCon;
  23. public MainPage()
  24. {
  25. this.InitializeComponent();
  26. }
  27. [Table("Students")]
  28. public sealed class Student
  29. {
  30. public string Name { get; set; }
  31. public int RollNo { get; set; }
  32. public string Faculty { get; set; }
  33. }
  34. //Creates First Database
  35. private async void btnCreateFirstDb_Click(object sender, RoutedEventArgs e)
  36. {
  37. try
  38. {
  39. dbCon = new SQLiteAsyncConnection(ApplicationData.Current.LocalFolder.Path + "\\FirstDatabase.sqlite");
  40. await dbCon.CreateTableAsync<Student>();
  41. var student = new Student
  42. {
  43. Name = "Ram",
  44. RollNo = 1,
  45. Faculty = "Physics"
  46. };
  47. await dbCon.InsertAsync(student);
  48. }
  49. catch (Exception ex)
  50. {
  51. Debug.WriteLine(ex.ToString());
  52. }
  53. }
  54. //Creates Second Database
  55. private async void btnCreateSecondDb_Click(object sender, RoutedEventArgs e)
  56. {
  57. try
  58. {
  59. SQLiteAsyncConnection dbCon = new SQLiteAsyncConnection(ApplicationData.Current.LocalFolder.Path + "\\SecondDatabase.sqlite");
  60. await dbCon.CreateTableAsync<Student>();
  61. var student = new Student
  62. {
  63. Name = "Shyam",
  64. RollNo = 2,
  65. Faculty = "Biology"
  66. };
  67. await dbCon.InsertAsync(student);
  68. }
  69. catch (Exception ex)
  70. {
  71. Debug.WriteLine(ex.ToString());
  72. }
  73. }
  74. //Merging two databases. Inserts values from Second database to First Database
  75. private async void btnMerge_Click(object sender, RoutedEventArgs e)
  76. {
  77. try
  78. {
  79. string firstDbPath = ApplicationData.Current.LocalFolder.Path + "\\FirstDatabase.sqlite";
  80. string secondDbPath = ApplicationData.Current.LocalFolder.Path + "\\SecondDatabase.sqlite";
  81. await dbCon.ExecuteAsync("ATTACH DATABASE '" + firstDbPath + "' AS firstDB;");
  82. await dbCon.ExecuteAsync("ATTACH DATABASE '" + secondDbPath + "' AS secondDB");
  83. string query = "INSERT OR REPLACE INTO firstDB.Students ("
  84. + "Name, RollNo, Faculty) "
  85. + "SELECT Name, RollNo, Faculty "
  86. + "FROM secondDB.Students";
  87. await dbCon.ExecuteAsync(query);
  88. }
  89. catch (Exception ex)
  90. {
  91. Debug.WriteLine(ex.ToString());
  92. }
  93. }
  94. }
  95. }
Step 4

Run the application.

Click on Create First Database button. You will see the first SQLite database created in local folder. (This PC > C > Users > [Your User Name] > AppData > Local > Packages > [App package name] > LocalState)




Click on Create Second Database button. You will see the second SQLite database created in local folder. (This PC > C > Users > [Your User Name] > AppData > Local > Packages > [App package name] > LocalState)



Finally Click on Merge two databases button, you will see the values from second database merged into first database.



You can get the complete project from GitHub https://github.com/olikishor/SQLiteMerge