C# 如何通过隔离存储访问SQLite数据库?
我试图通过IsolatedStorage访问我的新SQLite数据库,但当我这样做时:C# 如何通过隔离存储访问SQLite数据库?,c#,sqlite,windows-phone-8,isolatedstorage,C#,Sqlite,Windows Phone 8,Isolatedstorage,我试图通过IsolatedStorage访问我的新SQLite数据库,但当我这样做时: new SQLiteConnection(_dbpath); 找不到数据库。 以下是我创建文件的代码: IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication(); if (!isoStore.FileExists("TestDB.sqlite")) { isoStore.CreateFile("Te
new SQLiteConnection(_dbpath);
找不到数据库。
以下是我创建文件的代码:
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
if (!isoStore.FileExists("TestDB.sqlite"))
{
isoStore.CreateFile("TestDB.sqlite");
}
_dbpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "SportInDB.sqlite");
在创建新的SQLiteConnection时,我是否遗漏了任何内容?举个简单的例子怎么样?它创建一个数据库,其中包含一个与类问题匹配的表
我使用silverlight应用程序,但没有Windows.Storage.ApplicationData.Current.LocalFolder.path当我这样做时,找不到新的连接路径…但是getfileasync返回了正确的值..如果您的8.0 WP silverlight或8.1 WP silverlight中没有Windows.Storage.ApplicationData.Current.LocalFolder.path,那么您就有一个很大的问题。你需要先解决这个问题。从模板创建一个空白的silver light应用程序,你能在那里访问它吗,或者它的文本下有红线吗?是的,这是可以的,但我有同样的问题。
// namespaces
using SQLite;
using System.IO;
using System.IO.IsolatedStorage;
using System.Threading.Tasks;
// define a class like table to create
public class Question
{
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public int Id { get; set; }
public int Status { get; set; }
}
public partial class MainPage : PhoneApplicationPage
{
string dbPath = "";
// Constructor
public MainPage()
{
InitializeComponent();
// combine the local folder with the file name of the database
dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
CreateDBTable();
}
// from the MSDN article for getting SQLite to work
private async Task<bool> FileExists(string fileName)
{
var result = false;
try
{
var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
result = true;
}
catch
{
}
return result;
}
// if the file doesn't exist, create it with the db.CreateTable
public void CreateDBTable()
{
if (!FileExists("db.sqlite").Result)
{
using (var db = new SQLiteConnection(dbPath))
{
db.CreateTable<Question>();
}
}
}
}