Silverlight3 database operations (5)

Source: Internet
Author: User

1. for reference to namespaces in the Silverlight project, refer to the corresponding namespaces as follows:

2. The content of mainpage. XAML is as follows:

 1:  <UserControl
 2:      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 3:      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4:      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
 5:      mc:Ignorable="d" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="funsl.com.MainPage"
 6:      d:DesignWidth="640" d:DesignHeight="480">
 7:    <Grid x:Name="LayoutRoot">
 8:        <data:DataGrid x:Name="GirdView1" Margin="116,88,236,193"/>
9: <button X: Name = "bt_add" Height = "31" horizontalalignment = "Left" margin = "60, 0, 0,109 "verticalignment =" bottom "width =" 118 "content =" add "/>
10: <button X: Name = "bt_edit" Height = "31" margin = "214,0, 308,109" verticalignment = "bottom" content = "modify"/>
11: <button X: Name = "bt_del" Height = "31" horizontalalignment = "right" margin = "0, 0, 151,109 "verticalignment =" bottom "width =" 118 "content =" delete "/>
12:    </Grid>
13:  </UserControl>

A gridview control is defined to display data. The three buttons are add, modify, and delete.

3. The following is the code section of the mainpage. XAML. CS file. That is, we started to operate the database through. Net Ria service.

 1:  using System;
 2:  using System.Collections.Generic;
 3:  using System.Linq;
 4:  using System.Net;
 5:  using System.Windows;
 6:  using System.Windows.Controls;
 7:  using System.Windows.Documents;
 8:  using System.Windows.Input;
 9:  using System.Windows.Media;
10:  using System.Windows.Media.Animation;
11:  using System.Windows.Shapes;
12: // reference funsl.com. web space
13:  using funsl.com.Web;
14:  
15:  namespace funsl.com
16:  {
17:      public partial class MainPage : UserControl
18:      {
19: // define a domain data class, which is defined in funsl.com. Web.
20:          FunSLDomainContext funSLContext;
21:  
22:          public MainPage()
23:          {
24:              InitializeComponent();
25:  
26: // instantiate funsldomaincontext
27:              funSLContext = new FunSLDomainContext();
28:  
29:              LoadDataAndDisplay();
30:  
31: // register the button click event for the Add function
32:              bt_add.Click += new RoutedEventHandler(bt_add_Click);
33: // register the button click event for the modification function
34:              bt_edit.Click += new RoutedEventHandler(bt_edit_Click);
35: // register the delete button to click the event
36:              bt_del.Click += new RoutedEventHandler(bt_del_Click);
37:  
38:          }
39:  
40: // This method displays all data
41:          void LoadDataAndDisplay()
42:          {
43: // bind the data source of the gridview control to the domaincontext
44:              GirdView1.ItemsSource = funSLContext.FunSLUsers;
45: // start loading data
46:              funSLContext.Load(funSLContext.GetFunSLUsersQuery());
47:          }
48:  
49: // Add a job
50:          void bt_add_Click(object sender, RoutedEventArgs e)
51:          {
52: // create a new funslusers and set the relevant values. Modify the values for different materials.
53:              FunSLUsers newUser = new FunSLUsers();
54:              newUser.userId = "FunSl.User";
55:              newUser.age = 25;
56:              newUser.address = "HK";
57:  
58: // Add the new funslusers to domaincontext
59:              funSLContext.FunSLUsers.Add(newUser);
60: // update the changes to domaincontext to the database
61:              funSLContext.SubmitChanges();
62:  
63: // prompt
64: MessageBox. Show ("submitted ");
65:          }
66:  
67: // Modification
68:          void bt_edit_Click(object sender, RoutedEventArgs e)
69:          {
70: // reset the funslusers instance in dataview.
71:              FunSLUsers user = (FunSLUsers)GirdView1.SelectedItem;
72:  
73: // here I want to change the userid of the selected user to eidted
74:              user.userId = "edited";
75:  
76: // update the modification to the database
77:              funSLContext.SubmitChanges();
78:  
79: // prompt
80: MessageBox. Show ("modified ");
81:          }
82:  
83: // delete a job
84:          void bt_del_Click(object sender, RoutedEventArgs e)
85:          {
86: // reset the funslusers instance in dataview.
87:              FunSLUsers user = (FunSLUsers)GirdView1.SelectedItem;
88:  
89: // remove a user from domaincontext
90:              funSLContext.FunSLUsers.Remove(user);
91:  
92: // update the modification to the database
93:              funSLContext.SubmitChanges();
94:          }
95:      }
96:  }

By now, the basic application has been completed. net Ria service features are not described. It is designed for beginners in this series, so I do not want to make more complex introductions. I hope this series of articles can be. net Ria Serivce is helpful.

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.