本地数据库与SQLite在Windows phone 8

本文关键字:Windows phone SQLite 数据库 | 更新日期: 2023-09-27 18:12:31

我在Windows phone 8中创建了一个带有本地数据库的测试应用程序,但我得到错误,我的项目无法创建我的sqlite数据库

你能帮我吗?

运行时错误如下:

System.Reflection类型的异常。TargetInvocationException'在mscorlib.ni.dll中发生,但未在用户代码中处理

附加信息:异常已被调用的目标抛出。

在这一行中:val = m.GetValue(obj, null);

Person class:

namespace PhoneApp4
{
    public class person
    {
        [SQLite.AutoIncrement , SQLite.PrimaryKey]
        public int ID { get; set; }
        public string FullName { get; set; }
    }
}

mainpage.xaml.cs:

namespace PhoneApp4
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }
        private async void BTO_save_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, "dbsqlite.sqlite"), true);
            person person = new person
            {
                FullName = TB_save.Text
            };
            await conn.InsertAsync(person);
        }
        private async void BTO_search_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, "people.db"), true);
            var query = conn.Table<person>().Where(x => x.ID == Convert.ToInt32(TB_search.Text));
            var result = await query.ToListAsync();
            foreach (var item in result)
            {
                LB_search.Items.Add(item.ID.ToString() + "    " + item.FullName.ToString());
            }
        }

app.xaml.cs:

private void Application_Launching(object sender, LaunchingEventArgs e)
{
    string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "dbsqlite.sqlite");
        using (var db = new SQLiteConnection(dbPath))
        {
            db.CreateTable<person>();
        }
}
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
    string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "dbsqlite.sqlite");
    if (FileExists("dbsqlite.sqlite").Result)
    {
        using (var db = new SQLiteConnection(dbPath))
        {
            db.CreateTable<person>();
        }
    }
}
// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
    string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "dbsqlite.sqlite");
    if (!FileExists("dbsqlite.sqlite").Result)
    {
        using (var db = new SQLiteConnection(dbPath))
        {
            db.CreateTable<person>();
        }
    }
}

本地数据库与SQLite在Windows phone 8

查看这个示例http://code.msdn.microsoft.com/SQLite-for-Windows-Phone-8-8ff3beaf

 private async void Application_Launching(object sender, LaunchingEventArgs e) 
        { 
            try 
            { 
                await ApplicationData.Current.LocalFolder.GetFileAsync("UniversityDB.db"); 
                Connection = new SQLiteAsyncConnection("UniversityDB.db"); 
            } 
            catch (FileNotFoundException) 
            { 
                DataService.CreateDbAsync(); 
            } 
        }

public static async void CreateDbAsync() 
        { 
            App.Connection = new SQLiteAsyncConnection("UniversityDB.db"); 
            var universityTask = App.Connection.CreateTableAsync<University>(); 
            var studentTask = App.Connection.CreateTableAsync<Student>(); 
            await Task.WhenAll(new Task[] { universityTask, studentTask }); 
        }