繁体   English   中英

Windows Phone 8中使用SQLite的本地数据库

[英]Local database with SQLite in Windows phone 8

我在Windows Phone 8中使用本地数据库创建了一个测试应用程序,但出现错误,我的项目无法创建我的sqlite数据库

你能帮助我吗 ?

运行时错误是这样的:

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

附加信息:调用的目标已引发异常。

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

Person

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>();
        }
    }
}

请参阅此示例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 }); 
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM