【问题标题】:Local database with SQLite in Windows phone 8Windows phone 8 中使用 SQLite 的本地数据库
【发布时间】:2014-07-12 08:05:10
【问题描述】:

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

你能帮帮我吗?

运行时错误是这样的:

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

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

在这一行: 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>();
        }
    }
}

【问题讨论】:

  • 你使用的是哪个 SQLite 库?
  • 你可以试试这个new tutorial 并反馈吗?

标签: c# wpf sqlite windows-phone-8


【解决方案1】:

查看此示例 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 }); 
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 2015-11-27
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多