当前位置: 动力学知识库 > 问答 > 编程问答 >

c# 4.0 - How to Set User login Windows Phone 8 using sqlite database?

问题描述:

Hi i am using windows phone 8 app. i want to set existing user login, i can add user registration but i can't do user login my code is give below.

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

}

}

}

I am using your code.I got error The system cannot find the file specified. (Exception from HRESULT: 0x80070002)

网友答案:

ok so do this ....

p

ublic 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);

                        }
               }
       }

make a class like above code for your database connection and call this at your application startup like this dal.ConnectToDB();

then in your loginpage do like this...

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
           }
        }
网友答案:

you can try this ..

        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
           }
        }
网友答案:

Hi i got solution in my question..,

      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");
            }
        }
分享给朋友:
您可能感兴趣的文章:
随机阅读: