Operate a local database

Source: Internet
Author: User
1. When creating an object, pay attention to adding the Table and Column features. The summary course class summary [Table] indicates that the class will become a tablepublicclassCourse: INotifyPropertyChanged, INotifyPropertyChanging {[Column (IsVersiontrue)] table Column privat;

1. when creating an object, note that adding the Table and Column features // summary // Course class // summary [Table] // indicates that the class will become a table public class Course: INotifyPropertyChanged, INotifyPropertyChanging {[Column (IsVersion = true)] // private Binary _ version of the table Column;

1. Add the Table and Column features to create objects.

 
////// Course type ///   [Table]// Indicates that the class will become a table public class Course: INotifyPropertyChanged, INotifyPropertyChanging {[Column (IsVersion = true)]// Private Binary _ version; private int _ id;[Column (IsPrimaryKey = true, IsDbGenerated = true)] // table Column, primary key, automatically generatedPublic int Id {get {return _ id;} set {if (_ id! = Value) {RaiseProtertyChanging ("Id"); _ id = value; RaisePropertyChanged ("Id") ;}} private string _ name;[Column]Public string Name {get {return _ name;} set {if (_ name! = Value) {RaiseProtertyChanging ("Name"); _ name = value; RaisePropertyChanged ("Name") ;}} private string _ location;[Column]Public string Location {get {return _ location;} set {if (_ location! = Value) {RaiseProtertyChanging ("Location"); _ location = value; RaisePropertyChanged ("Location") ;}} public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged (string propertyName) {if (PropertyChanged! = Null) {PropertyChanged (this, new PropertyChangedEventArgs (propertyName) ;}} public event PropertyChangingEventHandler PropertyChanging; private void RaiseProtertyChanging (string propertyName) {if (PropertyChanging! = Null) {PropertyChanging (this, new PropertyChangingEventArgs (propertyName ));}}}
2. Create a class that inherits DataContext and encapsulates this. GetTable () Method. Otherwise, an error is returned when you create a database.
Public class MyDataContext: DataContext {// connection character public const string ConnectionString = "Data Source = isostore:/MyDb. sdf "; // constructor public MyDataContext (): base (ConnectionString) {if (! This. DatabaseExists () {// create the database this. CreateDatabase ();}}// It must exist; otherwise, the database creation error is returned: DataContext does not exist Table public Table
  
   
CourseTable {get {return this. GetTable
   
    
();}}
   
  }

3. Page front-end code
         
             
                  
                   
               
              
                  
                   
                   
               
              
                 
    
                        
     
                            
                               
                                    
                                     
                                     
                                 
                                
                                
                                
                            
                       
                                              
          
 

4. Page background code

Add: Call GetTable (). InsertOnSubmit (T) method, and finally call SubmitChanges () to submit data to the database.

Delete: GetTable (). DeleteOnSubmit (T) method, and finally call SubmitChanges () to submit data to the database.

Edit: Call SubmitChanges ()

The data source bound to the page must beObservableCollection type

Public partial class MainPage: PhoneApplicationPage {Private ObservableCollection
  
   
Courses;
  Private DataContext _ data; // constructor public MainPage () {InitializeComponent (); // create a database // CreateDatabase (); _ data = new MyDataContext (); // initialize the data InitData ();} private void CreateDatabase () {_ data = new MyDataContext (); if (! _ Data. DatabaseExists () {_ data. CreateDatabase () ;}} private void InitData () {Courses = new ObservableCollection
 
  
{New Course {Name = "e-commerce", Location = "teaching building 101"}, new Course {Name = "Psychology", Location = "teaching building 101 "}, new Course {Name = "Advanced Mathematics", Location = "teaching building 101"}, new Course {Name = "network marketing", Location = "teaching building 101 "},}; foreach (var c in Courses ){
  _ Data. GetTable
   
    
(). InsertOnSubmit (c); // insert a database
   }
  _ Data. SubmitChanges (); this. CourseList. ItemsSource = Courses;} // Add event private void Button_Click (object sender, RoutedEventArgs e) {Course c = new Course () {Name = "Customer Relationship Management", Location = "teaching building 401 "}; courses. add (c );
  _ Data. GetTable
   
    
(). InsertOnSubmit (c); _ data. SubmitChanges ();
   } // Edit event private void Button_Click_1 (object sender, RoutedEventArgs e) {Course c = Courses. first (s => s. name = "network marketing"); c. location = "Edit teaching building ";
  _ Data. SubmitChanges ();} // Delete the event private void Button_Click_2 (object sender, RoutedEventArgs e) {Course c = Courses. first (s => s. name = "e-commerce"); Courses. remove (c );
  _ Data. GetTable
   
    
(). DeleteOnSubmit (c); _ data. SubmitChanges ();
   }}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.