C# Windows phone 8中使用SQLite的本地数据库
我在WindowsPhone8中创建了一个带有本地数据库的测试应用程序,但出现错误,我的项目无法创建sqlite数据库 你能帮我吗 运行时错误如下: mscorlib.ni.dll中发生“System.Reflection.TargetInvocationException”类型的异常,但未在用户代码中处理 附加信息:调用目标已引发异常 此行:val=m.GetValue(obj,null)C# Windows phone 8中使用SQLite的本地数据库,c#,wpf,sqlite,windows-phone-8,C#,Wpf,Sqlite,Windows Phone 8,我在WindowsPhone8中创建了一个带有本地数据库的测试应用程序,但出现错误,我的项目无法创建sqlite数据库 你能帮我吗 运行时错误如下: mscorlib.ni.dll中发生“System.Reflection.TargetInvocationException”类型的异常,但未在用户代码中处理 附加信息:调用目标已引发异常 此行:val=m.GetValue(obj,null) Personclass: namespace PhoneApp4 { public class
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());
}
}
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>();
}
}
}
看到这个样本了吗
及
public static async void CreateDbAsync()
{
App.Connection=新的SQLiteAsyncConnection(“UniversityDB.db”);
var universityTask=App.Connection.CreateTableAsync();
var studentTask=App.Connection.CreateTableAsync();
等待任务.WhenAll(新任务[]{universityTask,studentTask});
}
您使用的是哪个SQLite库?您可以尝试一下并报告吗?
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 });
}