Skip to main content

SQLite Database for Windows 8 App : Adding existing SQLite DB file




Hi Guys ...

This Post is about adding an existing SQLite database file to the Windows Metro App project.
As I think "using SQLite" is the best way to create local database for Windows Rt applications.Actually SQLite is very easy to learn but I really think if Microsoft SQL Server CE supports windows rt applications it will more fun. .Isnt it ?. :)



  • The first step is to copy your database file in to the Visual Studio project and, go to the Properties window of the DB file, set the Build action to Content.

  • Add following code to the MainPage.cs
  •  private async void UpDatabase()  
     {  
       bool isDatabaseExisting = false;  
       try  
       {  
         StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("appdb.db");  
         isDatabaseExisting = true;  
       }  
       catch  
       {  
         isDatabaseExisting = false;  
       }  
       if (!isDatabaseExisting)  
       {  
         StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync("appdb.db");  
         await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);  
       }  
     }  
    

  • Call this method when start up the application .You can call the method inside the MainPage () 
 public MainPage()  
     {  
       this.InitializeComponent();  
       UpDatabase();  
     }  

What is Happening when the code run?? 

Check if the file already exists or not. If it doesn’t exist, we get a reference to the file embedded in the Visual Studio project (called appdb.db) and then we copy it in the local storage. This way the file is copied in the root of the local storage


Hope This will help You Guys ....
ThankYou!!!!!!!!!!

Facebook Page
Project.net Group




Comments

Popular posts from this blog

Multithreading C# : Part 01

Multithreading with C# Think about old days, you have one central processing unit (CPU) in your pc which is capable of executing one operation at a time. If we have 5 operations to run then we have to wait till one operation is completed and then only other one can start. What will happen if the running operation has a bug and got stuck, then whole computer going to be freeze and useless unless we restart it. This is a huge problem. So if we can run multiple operation in the same time that would be great because it will solve this problem.

Load an Addin into DraftSight

These days i am developing two plugins for Autocad 2012 &  DraftSight 2013.so when i was developing the plugin i got in to big trouble.Normally in the Autocad , we can load the .dll file simply typing the command "NETLOAD" but in the Draftsight i could not able to do that. It said  " D:\c \projects\c projects\DSAddinCSharp1\ DSAddinCSharp1\bin\Debug\ DSAddinCSharp1.dll is not a valid add-in "