2013-06-24 52 views
0

嗨,我正在使用Windows Phone 8應用程序。我想設置現有的用戶登錄,我可以添加用戶註冊,但我不能做用戶登錄我的代碼如下。如何設置用戶登錄Windows Phone 8使用sqlite數據庫?

public partial class LoginPage : PhoneApplicationPage 
{ 
    public LoginPage() 
    { 
     InitializeComponent(); 

    } 
    public static class dal 
    { 

     public static SQLiteAsyncConnection connection; 
     public static bool isDatabaseExisting; 

     public static async void ConnectToDB() 
     { 
      try 
      { 
       StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("Bestin.sqlite"); 
       isDatabaseExisting = true; 
      } 
      catch (Exception ex) 
      { 
       isDatabaseExisting = false; 
      } 

      if (!isDatabaseExisting) 
      { 
       try 
       { 
        StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync("Bestin.sqlite"); 
        await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder); 
        isDatabaseExisting = true; 
       } 
       catch (Exception ex) 
       { 
        isDatabaseExisting = false; 
       } 
      } 

      if (isDatabaseExisting) 
      { 

       connection = new SQLiteAsyncConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, "Bestin.sqlite"), true); 

      } 
     } 
    } 

    private void Click_Login(object sender, RoutedEventArgs e) 
    { 
     dal.ConnectToDB(); 
     var query = dal.connection.QueryAsync<Task>("SELECT * FROM Task Where Email=" + "\'" + txtEmailaddress.Text + "\'" + "and Password=" + "\'" + txtPassword.Password + "\'").Result; 

     if (query == null) 
     { 
      // invalid Login credentials 
     } 
     else 
     { 
      // do login 
     } 
    } 

} 

我使用你的code.I遇到錯誤該系統找不到指定的文件。 (異常來自HRESULT:0x80070002)

回答

0

確定,所以做到這一點....

public static class dal 
{ 
    public static SQLiteAsyncConnection connection; 
    public static bool isDatabaseExisting; 

    public static async void ConnectToDB() 
    { 
     try 
     { 
      StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("Bestin.sqlite"); 
      isDatabaseExisting = true; 
     } 
     catch (Exception ex) 
     { 
      isDatabaseExisting = false; 
     } 

     if (!isDatabaseExisting) 
     { 
      try 
      { 
       StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync("Bestin.sqlite"); 
       await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder); 
       isDatabaseExisting = true; 
      } 
      catch (Exception ex) 
      { 
       isDatabaseExisting = false; 
      } 
     } 

     if (isDatabaseExisting) 
     { 

      connection = new SQLiteAsyncConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, "Bestin.sqlite"), true); 

     } 
    } 
} 

做出類似上面代碼的類爲你的數據庫連接並在您的應用程序啓動時調用此函數,如dal.ConnectToDB();

然後在loginpage做這樣的...

private void Click_Login(object sender, RoutedEventArgs e) 
{ 

    var query = dal.connection.QueryAsync<Task>("SELECT * FROM Task Where Email=" + "\'" + txtEmailaddress.Text + "\'" + "and Password=" + "\'" + txtPassword.Password + "\'").Result; 

    if(query == null) 
    { 
    // invalid Login credentials 
    }  
    else 
    { 
    // do login 
    } 
} 
+0

試試這個,讓我知道羯羊它的工作或不... –

+0

我使用你的代碼,我得到錯誤:'isDatabaseExisting'這個名字在當前上下文中不存在。這唯一的錯誤 – raja

+0

我已經更新了我的解決方案請添加公共靜態布爾isDatabaseExisting;就在公共靜態SQLiteAsyncConnection連接下方; –

0

你可以試試這個..

 private void Click_Login(object sender, RoutedEventArgs e) 
     { 
      dbConn = new SQLiteConnection(DB_PATH); 
      var query = dbconn.QueryAsync<Task>("SELECT * FROM Task Where Email=" + "\'" + txtEmailaddress.Text + "\'" + "and Password=" + "\'" + txtPassword.Password + "\'").Result; 

      if(query == null) 
      { 
      // invalid Login credentials 
      }  
      else 
      { 
      // do login 
      } 
     } 
+0

嗨感謝您的重播,我想你的代碼中,我得到了錯誤如下:名稱'dbconn'在當前上下文中不存在 – raja

+0

dbconn在代碼中如何工作? 您必須聲明它的地方,同樣會在這裏工作 或者你可以這樣寫 SQLiteConnection dbconn =新SQLiteConnection(DB_PATH); –

+0

已經聲明dbConn:dbConn = new SQLiteConnection(DB_PATH);這是對的? – raja

0

嗨,我有我的問題解決方案..,

 using (var dbConn = new SQLiteConnection(DB_PATH)) 
     { 
      var existing = dbConn.Query<Userlist>("select * from Userlist Where Email=" + "\'" + txtEmailaddress.Text + "\'" + "and Password=" + "\'" + txtPassword.Text + "\'").FirstOrDefault(); 

      if (existing != null) 
      { 

       NavigationService.Navigate(new Uri("/Input.xaml?selectedItem=", UriKind.Relative)); 

      } 
      else 
      { 
       MessageBox.Show("invalid login"); 
      } 
     } 
相關問題