Windows Phone local database (sqlce) series translation Article index and example programs

Source: Internet
Author: User

I have attempted to translate some simple basic English tutorials with poor English skills. I guess there are many mistakes. I will arrange them here and provide a simple example program. Please advise.

  1. Windows Phone local database (sqlce): 1. Introduction)
  2. Windows Phone local database (sqlce): 2. LINQ to SQL)
  3. Windows Phone local database (sqlce): 3. [Table] attribute)
  4. Windows Phone local database (sqlce): 4. [column] attribute)
  5. Windows Phone local database (sqlce): 5. [Association] attribute)
  6. Windows Phone local database (sqlce): 6. [Index] attribute)
  7. Windows Phone local database (sqlce): 7. database mapping)
  8. Windows Phone local database (sqlce): 8. datacontext)
  9. Windows Phone local database (sqlce): 9. connection strings)
  10. Windows Phone local database (sqlce): 10. Create a database)
  11. Windows Phone local database (sqlce): 11. query the database using LINQ)
  12. Windows Phone local database (sqlce): 12. insert data)
  13. Windows Phone local database (sqlce): 13. Update Data)
  14. Windows Phone local database (sqlce): 14. delete data)

Here I provide a simple example of adding, deleting, modifying, and querying

First look at the basic interface, the following three buttons are respectively add, delete, modify

The following is the page for adding information

I have summarized the following steps:

1. Create a data table based on the actual situation. studenttable. CS is created Here. Write the required data table columns and define relevant attributes.

Before that, we need to add reference system. Data. LINQ and using system. Data. LINQ. Mapping in CS code.

The Code of studenttable. CS is as follows:

 1      [Table]//define a table 2      public class StudentTable 3      { 4          private string schoolId; 5   6          [Column(IsPrimaryKey = true, CanBeNull = false, DbType = "NVarChar(20) NOT NULL", AutoSync = AutoSync.OnInsert)] 7          public string SchoolId 8          { 9              get { return schoolId; }10              set { schoolId = value; }11          }12  13          private string name;14  15          [Column]16          public string Name17          {18              get { return name; }19              set { name = value; }20          }21  22  23          private string address;24          [Column]25          public string Address26          {27              get { return address; }28              set { address = value; }29          }30      }

For more information about data tables, see Windows Phone local database (sqlce): 3. [Table] attribute)

2. Define a database and create a new class named studentdatacontext. cs. Here, the using system. Data. LINQ and studentdatacontext must inherit from datacontext.

The Code of studentdatacontext. CS is as follows:

 1      public class StudentDataContext : DataContext 2      { 3          public static string ConnectionString = "Data Source=isostore:/StudentInfo.sdf"; 4          //"Data Source=isostore:/StudentInfo.sdf"; 5          public StudentDataContext(string connectionString) 6              : base(connectionString) 7          { } 8   9          public Table<StudentTable> Students10          {11              get12              {13                  return this.GetTable<StudentTable>();14              }15          }16  17      }

Note the connectionstring format. The basic format is string format:"Data Source = isostore:/directory/file. SDF ";

Directory/file. SDF is the custom path and database name.

For more information, see the msdn documentation http://msdn.microsoft.com/zh-cn/library/hh202861 (V = vs.92). aspx

3. Create a database. Generally, the database is created when the program starts. We choose to create the database in the application_launching method of APP. XAML. cs. Before creating the database, we need to check whether the database exists.

 1          private void Application_Launching(object sender, LaunchingEventArgs e) 2          { 3              using (StudentDataContext context = new StudentDataContext(StudentDataContext.ConnectionString)) 4              { 5                  if (!context.DatabaseExists()) 6                  { 7                      context.CreateDatabase(); 8                  } 9              }10          }

4. Now we can perform basic database operations. I define a linqhelper class to perform these operations. The Code is as follows:

1 public class linqhelper 2 {3 Public static list <studenttable> getstudentinfo () // query 4 {5 List <studenttable> ls = new list <studenttable> (); 6 using (studentdatacontext context = new studentdatacontext (studentdatacontext. connectionstring) 7 {8 var query = from P in context. students select P; 9 ls = query. tolist (); 10} 11 12 Return ls; 13} 14 15 public static void addstudentinfo (string schoolid, string name, string address) // Add 16 {17 using (studentdatacontext context = new studentdatacontext (studentdatacontext. connectionstring) 18 {19 studenttable S = new studenttable (); 20 s. schoolid = schoolid; 21 s. name = Name; 22 s. address = address; 23 context. students. insertonsubmit (s); 24 25 context. submitchanges (); 26} 27} 28 29 public static void delstudentinfo (string schoolid) // Delete 30 {31 using (studentdatacontext context = new studentdatacontext (studentdatacontext. connectionstring) 32 {33 var query = from P in context. students where p. schoolid = schoolid select P; 34 studenttable studenttodelete = query. firstordefault (); 35 36 context. students. deleteonsubmit (studenttodelete); 37 38 context. submitchanges (); 39} 40} 41 42 public static void updatestudentinfo (string oldschoolid, string name, string address) // change 43 {44 using (studentdatacontext context = new studentdatacontext (studentdatacontext. connectionstring) 45 {46 var query = from P in context. students where p. schoolid = oldschoolid select P; 47 studenttable studenttoupdate = query. firstordefault (); 48 49 studenttoupdate. name = Name; 50 studenttoupdate. address = address; 51 52 context. submitchanges (); 53} 54} 55}

Add related methods and parameters to the event processing functions of each button.

I am using data binding to bind data to ListBox. How can I refresh the current page, after the delete operation, you need to refresh the page to see that the ListBox item is deleted. Here, I use the method to navigate to this page.

1 NavigationService.Navigate(new Uri("/MainPage.xaml?guid=" + Guid.NewGuid(), UriKind.Relative));

I don't know if this method is a good solution. Thank you !!!

Source code download

 

Related Article

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.