zoukankan      html  css  js  c++  java
  • Windows Phone 7.5 Local SQL Database:Mango Local Database(SQL CE)

    DatabaseWindows Phone Mango Local Database(SQL CE) 系列

    http://windowsphonegeek.com/tips/Windows-Phone-Mango-Local-Database-SQL-CE--How-to-Delete-data

    by WindowsPhoneGeek

    This is the 13th post from the "Windows Phone Mango Local Database" series of short posts that will cover all you need to know in order  to get started using a Local Database in Windows Phone 7.1 Mango.  This time I am going to talk about how to update data  when working with a Windows Phone 7.1 Mango local database.

    Here is what else is included in this series:

    Updating data into the database is a three-step process.  First, query the database for the object that is to be updated. Then, modify the object as desired. Finally, call the SubmitChanges method to save the changes to the local database.

    NOTE: If you bind objects in the data context to controls on the page, the data context can be updated automatically based on user interaction. Then, the only step required is to call the SubmitChanges method at the desired time.

    NOTE: Data is not updated in the database until the SubmitChanges method is called.

    For reference you can also take a look at the full MSDN documentation.

    How to Update Data?

    Before we begin lets assume that we have the following database structure with two tables: Country and City:

    101-0

    The DataContext is as follows:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    public class CountryDataContext : DataContext
    {
        public CountryDataContext(string connectionString)
            : base(connectionString)
        {
        }
     
        public Table<Country> Countries
        {
            get
            {
                return this.GetTable<Country>();
            }
        }
     
        public Table<City> Cities
        {
            get
            {
                return this.GetTable<City>();
            }
        }
    }

    In the code sample below we will demonstrate the process of:

    • creating the data context
    • finding the target "City" that will be updated
    • updating the Name of the city to "Madrid"
    • finally we will call SubmitChanges() to save the changes back to the database
    private void UpdateCity(){    using (CountryDataContext context = new CountryDataContext(ConnectionString))    {        // find a city to update        IQueryable<City> cityQuery = from c in context.Cities where c.Name == "Barcelona" select c;        City cityToUpdate = cityQuery.FirstOrDefault();                  // update the city by changing its name        cityToUpdate.Name = "Madrid";          // save changes to the database        context.SubmitChanges();    }}

    How to insert Data?

    Before we begin lets assume that we have the following database structure with two tables: Country and City:

    101-0

    [Table]
    public class Country
    {
     //...
    }
    
    
    [Table]
    public class City
    {
        //...
    }

    The DataContext is as follows;

    public class CountryDataContext : DataContext
    {
        public CountryDataContext(string connectionString)
            : base(connectionString)
        {
        }
     
        public Table<Country> Countries
        {
            get
            {
                return this.GetTable<Country>();
            }
        }
     
        public Table<City> Cities
        {
            get
            {
                return this.GetTable<City>();
            }
        }
    }

    In the code sample below we will demonstrate the process explained above be crating and inserting two new related objects in the database - a country and a city. First, we create a new country instance and add it to the context (using the InsertOnSubmit method). Next, we create a new city instance, assign the country instance that we just create, and add it to the context. Finally we call the SubmitChanges method in order to actually save the changes to the database.

    private void AddCity()
    {
        using (CountryDataContext context = new CountryDataContext(ConnectionString))
        {
            // create a new country instance
            Country country = new Country();
            country.Name = "Spain";
            // add the new country to the context
            context.Countries.InsertOnSubmit(country);
    
            // create a new city instance
            City city = new City();
            city.Name = "Barcelona";
            // assing country
            city.Country = country;
            // add the new city to the context
            context.Cities.InsertOnSubmit(city);
    
            // save changes to the database
            context.SubmitChanges();
        }
    }

    How to Delete Data?

    Before we begin, lets assume that we have the following database structure with two tables: Country and City:

    101-0

    The DataContext is as follows:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    public class CountryDataContext : DataContext
    {
        public CountryDataContext(string connectionString)
            : base(connectionString)
        {
        }
      
        public Table<Country> Countries
        {
            get
            {
                return this.GetTable<Country>();
            }
        }
      
        public Table<City> Cities
        {
            get
            {
                return this.GetTable<City>();
            }
        }
    }

    In the code sample below we will demonstrate the process of:

    • creating the data context
    • finding the target "City" that will be delete
    • deleting the City from the data context
    • finally we will call SubmitChanges() to save the changes back to the database
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    private void DeleteCity()
    {
        using (CountryDataContext context = new CountryDataContext(ConnectionString))
        {
            // find a city to delete
            IQueryable<City> cityQuery = from c in context.Cities where c.Name == "Madrid" select c;
            City cityToDelete = cityQuery.FirstOrDefault();
            
            // delete city from the context
            context.Cities.DeleteOnSubmit(cityToDelete);
            // save changes to the database
            context.SubmitChanges();
        }
    }

    In this article I talked about inserting data when working with a Windows Phone 7.1 Mango local database. Stay tuned for the rest of the posts.

    You may also find interesting the following articles:

    做个快乐的自己。
  • 相关阅读:
    管理经济学
    第五章 文件管理
    内存管理
    第三章 进程调度与死锁
    操作系统概论 第二章
    计算机系统
    企业与政府信息资源管理
    信息资源管理的标准与法规
    议论文
    如何从大量的 url 中找出相同的 url
  • 原文地址:https://www.cnblogs.com/Jessy/p/2295439.html
Copyright © 2011-2022 走看看