Click here to Skip to main content
12,067,278 members (55,887 online)
Rate this:
 
Please Sign up or sign in to vote.
Hi everyone,

I am creating a database using the following code:

(name is constant string)

using SilverlightPhoneDatabase;
Database DB;
const string name = "MyDatabase";
 
private void CreateDBTest()
        {
            if (Database.DoesDatabaseExists(name) == false)
            {
                if (Database.CreateDatabase(name) != null)
                {
                    DB = Database.CreateDatabase(name);
                    DB.Save();
                }
            }
            else
            {
                tbDbExists.Text = "DB Already Exists...";
            }
        }

and then I create a table -

private void CreateTableTest()
        {
            DB = Database.OpenDatabase(name);
 
            DB.CreateTable<person>();
 
            if (DB.Table<person>() != null)
            {
                DB.Table<person>().Add(NewPerson());
                if (DB.Table<person>().Count == 1)
                {
                    tbTableCreated.Text = "Table Created...";
                }
            }
            else
            {
                tbTableCreated.Text = "Table Not Created...";
            }
        }</person>

where Person is a class with properties like FirstName, LastName etc.
and NewPerson is a function -

private Person NewPerson()
        {
            return new Person() 
            { 
                FirstName = "John", LastName = "Doe",
                Salary = 100          
             };
        }
Now when I create the database, it persists, and each time I try to create the database with same name, it says that it already exists. This is OK but when I create the table once, it doesn't persist for the next time. What should I do?

Thanks.
Posted 20-Jan-11 1:10am
Edited 20-Jan-11 1:14am
Slacker00790.1K
v2
Comments
   
DO NOT USE TEXT-SPEAK - TYPE THE ENTIRE WORD! Instead of "imp", type "important", otherwise, it may not be "important" enough for anyone to give you an answer.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Same problem. You are missing DB.Save().
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Ok
i got it
I've added DB.Save()in CreateTableTest()
and now my table is persistent.

But now i've another problem
that it is not deleting rows -


private void DeleteRecord()
        {
            DB = Database.OpenDatabase(name);
 
            var query = (from onePerson in DB.Table<person>()
                         where onePerson.Salary == 100
                         select onePerson);
 
            DB.Table<person>().Remove(query.ElementAt(0));
        }</person>

also i've tried -


DB.Table<person>().RemoveRange((person) => { return (person.Salary >= 1); });</person>

not giving any error,
but when i check, no row is deleted....
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160208.1 | Last Updated 15 Nov 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100