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

Imagine Cup 2014

Hi guys .. Get ready for imagine cup 2014 In NewZealand .In the next few days imagine cup 2013 finals will happen in Russia. So It is time to wear your thinking hat.You have one year to develop your marvelous ideas.  The Microsoft Imagine Cup is the world’s most prestigious student technology competition, bringing together student innovators from all over the world. If you have a great idea for a new app, bring it to life through Imagine Cup. Over the past 10 years, more than 1.65 million students from 190 countries have participated in the Imagine Cup. 

Visual Studio 2013 New Editor Features

In Visual Studio 2013, we have introduced new features that boost productivity and save time when working inside the Editor. Some of these are new features and some are the most popular extensions from  Productivity Power Tools . These features are a result of the feedback you gave us through  User Voice  requests, forum posts and Connect bugs. The MVP community also helped us pick some of these experiences. Our primary focus for the Editor in this version is to keep the developer in context as much as possible. This blog post describes capabilities that bring information to your fingertips and allow you to do as much as possible without leaving your place in code.