我在Windows Phone 8.1中使用Sqlite数据库设计了一个登录和注册页面。使用以下代码,我可以成功地从sqlite数据库中插入和检索值。但它只发生过一次。当我重新启动emualator时,我无法从数据库中检索值。
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
var dbpath = ApplicationData.Current.LocalFolder.Path + "/ebook.db";
var con = new SQLiteAsyncConnection(dbpath);
await con.CreateTableAsync<Register>();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
var dbpath = ApplicationData.Current.LocalFolder.Path + "/ebook.db";
var con = new SQLiteAsyncConnection(dbpath);
await con.CreateTableAsync<Register>();
Register m = new Register();
m.Name = text_reg.Text;
m.Password = text_password.Password;
string rd = "";
if (radio_male.IsChecked == true)
{
rd = "Male";
}
else
{
rd = "Female";
}
m.Gender = rd;
m.State = ((ComboBoxItem)combo_box.SelectedItem).Content.ToString();
await con.InsertAsync(m);
MessageDialog md = new MessageDialog("success");
await md.ShowAsync();
}
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
var dbpath = ApplicationData.Current.LocalFolder.Path + "/ebook.db";
var con = new SQLiteAsyncConnection(dbpath);
Register t = new Register();
string query = string.Format("select Name,Password from Register where Name='{0}' and Password='{1}'", text_user.Text, text_pass.Password);
List<Register> mylist = await con.QueryAsync<Register>(query);
if (mylist.Count == 1)
{
t = mylist[0];
}
if (t.Name == text_user.Text && t.Password == text_pass.Password)
{
this.Frame.Navigate(typeof(MainPage));
}
else
{
var messagedialog = new MessageDialog("Unsuccessful").ShowAsync();
}
}
}
答案 0 :(得分:3)
这是预期的行为。状态不会保留在Windows Phone模拟器中。当您重新启动它时,它会丢失您先前存储的所有数据(设置,已安装的应用程序以及您在存储上写入的任何内容)。
在实际的物理设备上不会发生这种情况。