2015-09-11 117 views
0

我正在开发一个使用SQLite的Windows Phone 8应用程序,并试图检查数据库是否存在,如果它不存在,它应该被创建。但我不断收到错误消息“System.windows.shapes.path不包含联合的定义”。还有其他方法可以做到吗或者我该如何改进?如何检查数据库是否存在于SQLite中?

public static string DB_PATH = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "ContactsManager.sqlite"));//DataBase Name 
    public App() 
    { 
     if (!CheckFileExists("ContactsManager.sqlite").Result) 
     { 
      using (var db = new SQLiteConnection(DB_PATH)) 
      { 
       db.CreateTable<Contacts>(); 
      } 
     } 
    } 

    private async Task<bool> CheckFileExists(string fileName) 
    { 
     try 
     { 
      var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName); 
      return true; 
     } 
     catch 
     { 
     } 
     return false; 
    } 
+1

Path.Combine在System.IO.Path定义。 –

回答

0

@Panagiotis Kanavos的评论是正确的,你已经使用错误的命名空间解决了路径类!

删除

using System.Windows.Shapes; // for silverlite 
using Windows.UI.Xaml.Shapes; // for winrt 

并添加

using System.IO; 
+0

谢谢你......它工作完美..我删除使用System.windows.shapes和替换它使用System.IO –

1

为什么你有一个Path.Combine一个Path.Combine?如果Path.Combine不能提供一个或两个参数,为什么不简单地连接两个字符串?

你有它2X:public static string DB_PATH = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "ContactsManager.sqlite"));

+1

Path.Combine *确实接受两个参数。这可能是打字错误或复制粘贴代码的部分 –

+0

这也可能是他的问题,因为编译器说:'System.windows.shapes.path不包含定义的组合或'我错了吗? – BendEg

+0

也许我是盲人,但具有一个字符串参数(https://msdn.microsoft.com/de-de/library/dd991142(v=vs.110).aspx)的方法是可用的,因为Windows Phone ** 8.1 ** – BendEg

1

您可以通过此检查:

public async Task<bool> isFilePresent(string fileName) 
{ 
return System.IO.File.Exists(string.Format(@"{0}\{1}", ApplicationData.Current.LocalFolder.Path, fileName); 
} 
2

你真的需要检查数据库中是否存在?我不知道windows phone,但在Windows中,只要您尝试将表添加到SQLite数据库中,如果数据库不存在,它就会创建它。如果你担心已经存在的表格,你可以使用:

CREATE TABLE IF NOT EXISTS tableName(...) 

(我试着问它作为评论,但我没有信誉)

相关问题