Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 20-Jan-11 0:14am
v2
Comments
#realJSOP 20-Jan-11 6:15am    
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().
 
Share this answer
 
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....
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900