Reputation: 1407
Any Samples on Local Database in windows Phone 7?i haven't work on it till now.So, Please give some idea on that.How to save data in windows phone 7.
Upvotes: 2
Views: 3917
Reputation: 25
First we have to create local database table.
namespace DatabaseSample.Db
{
[Table]
public class tblStudentDetails
{
[Column(CanBeNull = false)]
public string name
{
get;
set;
}
[Column(CanBeNull = false)]
public string std
{
get;
set;
}
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int id
{
get;
set;
}
}
}
After that we can create a database context
namespace DatabaseSample.Db
{
public class dbDataContext : DataContext
{
public dbDataContext(string connectionString)
: base(connectionString)
{
}
public Table<tblStudentDetails> studentDetails
{
get
{
return this.GetTable<tblStudentDetails>();
}
}
}
}
After that we can connect to the database and can insert value to the table
public class ConnectTable
{
private const string Con_String = @"isostore:/Db.sdf";
public ConnectTable()
{
using (Db.dbDataContext context = new Db.dbDataContext(Con_String))
{
if (!context.DatabaseExists())
{
// create database if it does not exist
context.CreateDatabase();
}
}
}
#region StudentDetails
public void AddToTableSDetails(string name,string standard)
{
using (Db.dbDataContext context = new Db.dbDataContext(Con_String))
{
Db.tblStudentDetails sd = new Db.tblStudentDetails();
sd.name = name;
sd.std = standard;
context.studentDetails.InsertOnSubmit(sd);
context.SubmitChanges();
}
}
public IList<Db.tblStudentDetails> GetSDetails()
{
IList<Db.tblStudentDetails> sList = null;
using (Db.dbDataContext context = new Db.dbDataContext(Con_String))
{
IQueryable<Db.tblStudentDetails> stQuery = from c in context.studentDetails select c;
sList = stQuery.ToList();
}
return sList;
}
/* public void DeleteSDetails()
{
using (Db.dbDataContext context = new Db.dbDataContext(Con_String))
{
IQueryable<Db.tblStudentDetails> stQuery = from c in context.studentDetails select c;
foreach (var value in stQuery)
{
context.studentDetails.DeleteOnSubmit(value);
}
context.SubmitChanges();
}
}*/
#endregion
}
Upvotes: 0
Reputation: 84744
The official documentation is quite rich in this area (apologies for the link dump):
There are also good overview documentation:
Upvotes: 4