Introduction
In this article we are going to explore how to use a local database with Windows Phone 7 and we will further discuss the database operations such as insert, select or delete the data to the local database from the Windows Phone 7. In this whole task we will use the property concept and LINQ concept of C# which will very helpful to us to accomplish such task to operate these operation with the local database from Windows Phone 7. For using the LINQ first of all you have to add the namespace named as System.data.linq to your reference folder which will be selected from the .NET library you will see also that from where you have to add it. In this article we will take you to the Class file separately for which we will create our own database by using LINQ. Now the operation shows you how to insert the data into the local database and how to select the data and further you can delete the data from the local database. Whatever the data we want to insert into the local database it will be inserted through the TextBox. Now it's time to getting started to accomplish such type of task you have to follow the steps given below:
Step 1 : In this step first of all we have to open a Windows Phone application; let us see how you will open it.
- Go to Visual Studio 2010
- File->New->Project
- Select the template named as silverlight for Windows Phone
- Select the Windows Phone application
- Give it a name as you want.
![]()

Step 2 : In this step you will see how to use the LINQ we have to add the reference of the namespace; let us see from where you have to add that references.
Go to the Solution Explorer and right-click on the reference folder.

You just see the namespace reference named System.data.linq; select it and click oK.

Select to add it then you see that the library has been added to the reference folder as you see in the figure below:

Step 3 : In this step you have to add the two class files named MyCity.cs and CityDataContext.cs; let us see from where you have to add them.



Step 4 : In this step we will write code for the MyCity.cs file in which we are going to make a table named MyCity; inside we are using the property and it's all done using LINQ. Let us see the code, which is given below.
MyCity.cs Code:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq.Mapping;
using System.Data.Linq;
namespace mylocaldatabse
{
[Table]
public class MyCity
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int C_ID
{
get;
set;
}
[Column(CanBeNull = false)]
public string C_Name
{
get;
set;
}
}
}
Step 5 : In this step you have to write the code for the second class file named CityDataContext.cs and the code of that file is given below.
CityDataContext.cs Code:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq;
namespace mylocaldatabse
{
public class CityDataContext : DataContext
{
public CityDataContext(string connectionString) : base(connectionString)
{
}
public Table<MyCity> Cities
{
get
{
return this.GetTable<MyCity>();
}
}
}
}
Step 6 : In this step you just see the code for the MainPage.xaml.cs file which is given below.
MainPage.xaml.cs Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Text;
namespace mylocaldatabse
{
public partial class MainPage : PhoneApplicationPage
{
// short connection string format
private const string Con_String = @"isostore:/amit.sdf";
// Constructor
public MainPage()
{
InitializeComponent();
textBox1.Text = "";
using (CityDataContext context = new CityDataContext(Con_String))
{
if (!context.DatabaseExists())
{
context.CreateDatabase();
}
}
}
public IList<MyCity> GetCities()
{
IList<MyCity> cityList = null;
using (CityDataContext context = new CityDataContext(Con_String))
{
IQueryable<MyCity> query = from c in context.Cities select c;
cityList = query.ToList();
}
return cityList;
}
private void AddMyCity()
{
using (CityDataContext context = new CityDataContext(Con_String))
{
MyCity city = new MyCity();
city.C_Name = textBox1.Text;
context.Cities.InsertOnSubmit(city);
context.SubmitChanges();
}
}
private void DeleteMyCity()
{
using (CityDataContext context = new CityDataContext(Con_String))
{
IQueryable<MyCity> cityQuery = from c in context.Cities where c.C_Name == textBox1.Text select c;
MyCity cityToDelete = cityQuery.FirstOrDefault();
context.Cities.DeleteOnSubmit(cityToDelete);
context.SubmitChanges();
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
this.AddMyCity();
MessageBox.Show("City has been added");
textBox1.Text = "";
}
private void button2_Click(object sender, RoutedEventArgs e)
{
IList<MyCity> cities = this.GetCities();
StringBuilder messageBuilder = new StringBuilder();
messageBuilder.AppendLine("Cities are:");
foreach (MyCity city in cities)
{
messageBuilder.AppendLine(city.C_Name);
}
MessageBox.Show(messageBuilder.ToString());
}
private void button3_Click(object sender, RoutedEventArgs e)
{
this.DeleteMyCity();
MessageBox.Show("City has been Deleted ");
}
}
}
Step 7 : In this step you just see the code for the MainPage.xaml file which is given below.







Harish GaikwadPosted Apr 3, 2015, 1:11 AM
just now i reffered ur code...its very usefull..please can u tell the database location to browse...?