Create an ADO.NET Data Base In Xamarin Android App Using Visual Studio 2015


Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available. Create SQLite database with ADO.NET.


  • Visual Studio 2015 Update 3.

The steps, mentioned below, are required to be followed in order to create ADO.NET database in the Xamarin Android app, using Visual Studio 2015.

Step 1

Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio or click (Ctrl+Shift+N).


Step 2

After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android). Now, give your Android app; a name (Ex:sample) and give the path of your project. Afterwards, click OK.


Step 3

Now, go to Solution Explorer. In Solution Explorer, get all the files and source code in your project. Now, select Resource-->Layout-->double click to open main.axml page. You need to select the source to write XAML code. Choose the Designer Window, if you want to design it, and you can design your app.


Step 4

After opening main.axml, file will open the main page designer. You can design the page, as per your desire.


Now, delete Default hello world button, go to the source panel and you can see the button coding. You need to delete it. After deleting XAML code, now delete C# button action code. Go to MainActivity.cs page. You need to delete the button code.

Step 5

Now, go to the toolbox Window. In the toolbox Window, get all the types of the tools and controls. You need to go to the toolbox Window. Now, scroll down. You will see all the tools and controls.

You need to drag and drop the button.


Step 6

You need to drag and drop the TextView.


Step 7

You need to drag and drop the TextView.


Step 8

Now, go to the properties Window. You need to edit the button's Id value and Text Value (EX: android:id="@+id/btnCreateDB" android:text="Create Database").


Step 9

Edit the TextView's Id value and Text Value (Ex: android:id="@+id/textView1"android:text="Results").


Step 10

Also, edit the TextView's Id value (Ex: android:id="@+id/txtResults").


Step 11

In this step, go to Main.axml page Source Panel. Note, the button's Id value.and also note TextView Id values.


  1. <LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1">  
  2.     <LinearLayout android:orientation="horizontal" android:minWidth="25px" android:minHeight="25px" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_weight=".2" android:gravity="center">  
  3.         <Button android:text="Create Database" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnCreateDB" /> </LinearLayout>  
  4.     <LinearLayout android:orientation="vertical" android:minWidth="25px" android:minHeight="25px" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_weight=".6" android:layout_marginLeft="5dp" android:layout_marginRight="5dp">  
  5.         <TextView android:text="Results" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView1" android:textColor="#fff" />  
  6.         <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtResults" android:layout_marginTop="5dp" /> </LinearLayout>  
  7.     <LinearLayout android:orientation="horizontal" android:minWidth="25px" android:minHeight="25px" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout3" android:layout_weight=".2" /> </LinearLayout>  

Step 12

In this step, add required reference into your app.

Go to Solution Explorer-->Reference-->Right Click-->Add Reference.


Step 13

Now, select Reference required by you.



Step 14

Now, you will see the Reference given below.

  1. Mono.Andriod  
  2. Mono.Data.SQLite  
  3. System  
  4. System.Core  
  5. System.Data  
  6. System.Xml  

Step 15

In this step, go to the MainActivity.cs page in Solution Explorer and add the namespace, mentioned below.

  1. using System.Data;  
  2. using System.IO;  
  3. using Mono.Data.Sqlite;  
  4. using System.Runtime.InteropServices;  
  5. using System.Threading.Tasks;  

Step 16

Now, write the code given below from OnCreate() MainActivity.cs.

  1. protected override void OnCreate(Bundle bundle) {  
  2.     base.OnCreate(bundle);  
  3.     // Set our view from the "main" layout resource   
  4.     SetContentView(Resource.Layout.Main);  
  5.     var btnCreate = FindViewById < Button > (Resource.Id.btnCreateDB);  
  6.     var txtResult = FindViewById < TextView > (Resource.Id.txtResults);  
  7.     var context = btnCreate.Context;  
  8.     var docsFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);  
  9.     var pathToDatabase = Path.Combine(docsFolder, "db_adonet.db");  
  10.     btnCreate.Click += async delegate {  
  11.         try {  
  12.             SqliteConnection.CreateFile(pathToDatabase);  
  13.             txtResult.Text = string.Format("Database created successfully - filename = {0}\n", pathToDatabase);  
  14.         } catch (IOException ex) {  
  15.             var reason = string.Format("Unable to create the database - reason = {0}", ex.Message);  
  16.             Toast.MakeText(context, reason, ToastLength.Long).Show();  
  17.         }  
  18.         txtResult.Text += await createTable(pathToDatabase);  
  19.     };  
  20. }  

Step 17

In this step, add async Task in MainActivity.cs page and write the code, mentioned below.

  1. private async Task < string > createTable(string path) {  
  2.         var connectionString = string.Format("Data Source={0};Version=3;", path);  
  3.         try {  
  4.             using(var conn = new SqliteConnection((connectionString))) {  
  5.                 await conn.OpenAsync();  
  6.                 using(var command = conn.CreateCommand()) {  
  7.                     command.CommandText = "CREATE TABLE People (PersonID INTEGER PRIMARY KEY AUTOINCREMENT, FirstName ntext, LastName ntext)";  
  8.                     command.CommandType = CommandType.Text;  
  9.                     await command.ExecuteNonQueryAsync();  
  10.                     return "Database table created successfully";  
  11.                 }  
  12.             }  
  13.         } catch (Exception ex) {  
  14.             var reason = string.Format("Failed to insert into the database - reason = {0}", ex.Message);  
  15.             return reason;  
  16.         }  
  17.     }  
  18.     // This is just a sample script. Paste your real code (javascript or HTML) here.  
  19. if ('this_is' == /an_example/) {  
  20.     of_beautifier();  
  21. else {  
  22.     var a = b ? (c % d) : e[f];  
  23. }  

Step 18

In this step, give the required permissions in your app.

Go to the Solution Explorer--> properties-->Right click-->Open.


Step 19

After opening properties options, select Android Manifest-->Required Permissions-->Check WRITE EXTERNAL_STORAGE.


Step 20

If you have an Android virtual device, run the app on it, else connect your Android phone and run the app on it.

Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.



After a few seconds, the app will start running on your phone. You will see your app is successful. Now, you can click Create Database Button.


You will see the database is created successfully.



This was the process of how to create ADO.NET database in Xamarin Android app, using Visual Studio 2015.

Up Next
    Ebook Download
    View all
    View all