Click here to Skip to main content
11,496,146 members (369 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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></person></person></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
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.
Same problem. You are missing DB.Save().
  Permalink  
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></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
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web02 | 2.8.150520.1 | Last Updated 15 Nov 2011
Copyright © CodeProject, 1999-2015
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