C# 使用文件选择器windows 8连接到sqlite db

C# 使用文件选择器windows 8连接到sqlite db,c#,windows-8,sqlite,microsoft-metro,windows-runtime,C#,Windows 8,Sqlite,Microsoft Metro,Windows Runtime,为什么我不能在使用文件保存选择器创建sqlite db后连接到它 下面的代码使用文件保存选择器创建sqlite数据库。我必须先在app文件夹中创建db,然后复制到picker文件 var savePicker = new FileSavePicker { SuggestedStartLocation = PickerLocationId.DocumentsLibrary, CommitButtonText = "Create new project" };

为什么我不能在使用文件保存选择器创建sqlite db后连接到它

下面的代码使用文件保存选择器创建sqlite数据库。我必须先在app文件夹中创建db,然后复制到picker文件

        var savePicker = new FileSavePicker
{
    SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
    CommitButtonText = "Create new project"
};
        savePicker.FileTypeChoices.Add("Data files", new List<string> { ".db" });
        savePicker.DefaultFileExtension = ".db";

        pickedFile = await savePicker.PickSaveFileAsync();

        if (pickedFile != null)
        {
            Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(pickedFile);

            var dbLocalPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "temp.db");

            var db = new SQLiteAsyncConnection(dbLocalPath);
            await db.CreateTableAsync<myTable>();

            await db.ExecuteAsync("INSERT INTO myTable (Record) VALUES ('1')");

            Debug.WriteLine(dbLocalPath);

            var db2 = new SQLiteAsyncConnection(dbLocalPath);
            var allRecords = await db2.QueryAsync<qvdb>("SELECT * FROM myTable");
            var countRecords = allRecords.Any() ? allRecords.Count : 0;
            Debug.WriteLine("There are " + countRecords);  // this works

            var localFile = await ApplicationData.Current.LocalFolder.GetFileAsync("temp.db");
            if (localFile != null) await localFile.CopyAndReplaceAsync(pickedFile);  // this works can open and see table in SQLite db browser

            Debug.WriteLine(pickedFile.Path);  // path is correct

            var db3 = new SQLiteAsyncConnection(pickedFile.Path);

            // ERROR //

            //   A first chance exception of type 'myApp.SQLiteException' occurred in myApp.exe
            //   A first chance exception of type 'myApp.SQLiteException' occurred in mscorlib.dll
            //   myApp.SQLiteException: Could not open database file: C:\Users\Gary\Documents\sql1.db (14)
            //   at myApp.SQLiteConnection..ctor(String databasePath) in c:\Users\Gary\Desktop\Current\myApp\SQLite.cs:line 108
            //   at myApp.SQLiteConnectionWithLock..ctor(SQLiteConnectionString connectionString) in c:\Users\Gary\Desktop\Current\myApp\SQLiteAsync.cs:line 441
            //   at myApp.SQLiteConnectionPool.Entry..ctor(SQLiteConnectionString connectionString) in c:\Users\Gary\Desktop\Current\myApp\SQLiteAsync.cs:line 373
            //   at myApp.SQLiteConnectionPool.GetConnection(SQLiteConnectionString connectionString) in c:\Users\Gary\Desktop\Current\myApp\SQLiteAsync.cs:line 406
            //   at myApp.SQLiteAsyncConnection.GetConnection() in c:\Users\Gary\Desktop\Current\myApp\SQLiteAsync.cs:line 44
            //   at myApp.SQLiteAsyncConnection.<>c__DisplayClass26`1.<QueryAsync>b__25() in c:\Users\Gary\Desktop\Current\myApp\SQLiteAsync.cs:line 255
            //   at System.Threading.Tasks.Task`1.InnerInvoke()
            //   at System.Threading.Tasks.Task.Execute()
            //--- End of stack trace from previous location where exception was thrown ---
            //   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            //   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            //   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
            //   at myApp.Pages.MainPage.<btnStartNew_Click>d__c3.MoveNext() in c:\Users\Gary\Desktop\Current\myApp\Pages\MainPage.xaml.cs:line 1587
        }

你试过使用非异步版本的SQLite吗?我有点好奇你为什么要这么做,为什么要将db文件存储在应用程序沙箱之外?这项功能实际上不是针对商业应用程序,而是针对我的公司,在我的公司,1-3个人需要使用我的应用程序一起构建数据库。我已经阅读了关于在共享网络驱动器上不使用SQLite的警告,但是考虑到我对并发性的需求很低,我想尝试一下。