神刀安全网

Xamarin Android: Create Login Using SQLite Database

What is SQLite?

SQLite database has methods to create, delete, and execute SQL commands and perform other common database management tasks. More details .

Let’s start,

Step 1

Open Visual Studio->New Project->Templates->Visual C#->Android->Blank App.

Select Blank App. Give the project name and the project location.

Xamarin Android: Create Login Using SQLite Database

Step 2

Next, go to Solution Explorer-> Project Name-> Resouces->layout. Right click Add and then a new dialog box will open. Select Android layout, give name for Newuser.axml and add the New Layout Page .

Xamarin Android: Create Login Using SQLite Database

Step 3

Go to Solution Explorer-> Project Name. Right click Add. Open new dialog box. Select Activity and give name for RegisterActivity.cs. Now, add the New Activity Class .

Xamarin Android: Create Login Using SQLite Database

Step 4

Next, we need to create the database table, so we create one Data Layer class. Go to Solution Explorer-> Project Name . Right click to Add . Open new dialog box. Select Class, give name for LoginTable.cs and after it, add New Class.

Xamarin Android: Create Login Using SQLite Database

Step 5

Go to Solution Explorer -> Project Name -> References. Right Click to manage Nuget Packages and open new dialog box. This dialog box searches SQLite. Install SQLite-Net Packages .

Xamarin Android: Create Login Using SQLite Database

Step 6

Next to Open Solution Explorer -> Project Name -> Resources -> Layout -> Main.axml,   click Open Design View . Here, create large text, two plain text and two buttons.

Xamarin Android: Create Login Using SQLite Database

Step 7

Next to Open Solution Explorer -> Project Name -> Resources -> Layout ->Newuser.axml,   click Open Design View . Here, create large text, two plain text and one button. 

Xamarin Android: Create Login Using SQLite Database

Step 8

Next to Open Solution Explorer -> Project Name ->LoginTable.cs, click Open CS code Page View and create table columns.

Xamarin Android: Create Login Using SQLite Database

C# Code

[PrimaryKey, AutoIncrement, Column("_Id")]      public int id { get; set; } // AutoIncrement and set primarykey      [MaxLength(25)]      public string username { get; set; }      [MaxLength(15)]      public string password { get; set; }   

Step 9

Next to Open Solution Explorer -> Project Name ->MainActivity.cs,   click Open CS code Page view and add below namespaces. First, we create database, so after OnCreate()  create new methodwith name of CreateDB(),

C# Code

public class MainActivity: Activity   {       EditText txtusername;       EditText txtPassword;       Button btncreate;       Button btnsign;       protected override void OnCreate(Bundle bundle)       {           base.OnCreate(bundle);           // Set our view from the "main" layout resource           SetContentView(Resource.Layout.Main);           // Get our button from the layout resource,           // and attach an event to it           btnsign = FindViewById < Button > (Resource.Id.btnlogin);           btncreate = FindViewById < Button > (Resource.Id.btnregister);           txtusername = FindViewById < EditText > (Resource.Id.txtusername);           txtPassword = FindViewById < EditText > (Resource.Id.txtpwd);           btnsign.Click += Btnsign_Click;           btncreate.Click += Btncreate_Click;           CreateDB();       }       private void Btncreate_Click(object sender, EventArgs e)       {           StartActivity(typeof(RegisterActivity));       }       private void Btnsign_Click(object sender, EventArgs e)       {           try           {               string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3"); //Call Database               var db = new SQLiteConnection(dpPath);               var data = db.Table < LoginTable > (); //Call Table               var data1 = data.Where(x => x.username == txtusername.Text && x.password == txtPassword.Text).FirstOrDefault(); //Linq Query               if (data1 != null)               {                   Toast.MakeText(this, "Login Success", ToastLength.Short).Show();               }               else               {                   Toast.MakeText(this, "Username or Password invalid", ToastLength.Short).Show();               }           }           catch (Exception ex)           {               Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();           }       }       public string CreateDB()       {           var output = "";           output += "Creating Databse if it doesnt exists";           string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3"); //Create New Database           var db = new SQLiteConnection(dpPath);           output += "/n Database Created....";           return output;       }   }   

Here, the database name is “user.db3”,

Step 10

Next Open Solution Explorer -> Project Name -> RegisterActivity.cs. Click Open CS code Page view then add below namespaces. Next write given below code. 

Xamarin Android: Create Login Using SQLite Database

Step 11
Press F5 or build and run the Application, first image is to register the new user and then the next image is to login the user.

Xamarin Android: Create Login Using SQLite Database

Xamarin Android: Create Login Using SQLite Database

Finally, we successfully created login using SQLite database.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Xamarin Android: Create Login Using SQLite Database

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址