如何检查是否创建了SQLite Db

时间:2013-11-12 08:28:59

标签: sqlite winrt-xaml

我正在使用下面的代码检查在App.Xaml.cs中创建数据库之前是否创建了数据库。

问题:

1)当我检查数据库是否在MainPage中再次创建时,GetIfFileExistsAsync返回false。

2)App.xaml.cs中的代码是否无效?


-- In MainPage.xaml 

private async void CheckDBCreated()
 {

     Task <bool> Tresult = GetIfFileExistsAsync(DBPath);
      bool result = await Tresult;          

    if (result == true)
    {
      txtBlkDBMsg.Text = result.ToString() +  " : Created";
     }
    else
     {
       txtBlkDBMsg.Text = result.ToString() +  " Not Created";
     }        
}


---1--in App.xaml.cs

public static string DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "customers.sqlite");


--2-- Checking if database created in App.xaml.cs

protected async override void OnLaunched(LaunchActivatedEventArgs args)
{

 Task <bool> Tresult = GetIfFileExistsAsync(DBPath);

 bool result = await Tresult;    

  if (result == true)
  {
      return;

  }
  else
  {
     CreateDBNow();
  }

}

--3---

 private async Task <bool> GetIfFileExistsAsync(string strDBPath)
 {

   try
   {          
       var dbFile = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(strDBPath);

        if (dbFile != null)
        {
           return true;
         }
        else
        {
           return false;
         }


     }
    catch(Exception ex)   
    {
      return false;

     }
  }



 private async void CreateDBNow()
  {

   using (var db = new SQLite.SQLiteConnection(DBPath))
    {
       // Create the tables if they don't exist

       db.CreateTable <Customer>();
       db.CreateTable <Sales>();
       db.CreateTable <TransactionLine>();    
      }

  }


0 个答案:

没有答案