Silverlight 使用WCF RIA 方式对数据库进行增删改查
Silverlight 使用WCF RIA 方式对数据库进行增山改查
使用的是Northwind数据库 DomainService1.cs中的文件内容: 分页标题 namespace SlWCfRiaDemo.Web{ using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Data; using System.Lin
使用的是Northwind数据库
![](http://www.silverlightchina.net/uploads/allimg/121114/1S6451X4-0.png)
DomainService1.cs中的文件内容:
namespace SlWCfRiaDemo.Web { using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Data; using System.Linq; using System.ServiceModel.DomainServices.EntityFramework; using System.ServiceModel.DomainServices.Hosting; using System.ServiceModel.DomainServices.Server; // Implements application logic using the NorthwindEntities context. // TODO: Add your application logic to these methods or in additional methods. // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access // Also consider adding roles to restrict access as appropriate. // [RequiresAuthentication] [EnableClientAccess()] public class DomainService1 : LinqToEntitiesDomainService<NorthwindEntities> { // TODO: // Consider constraining the results of your query method. If you need additional input you can // add parameters to this method or create additional query methods with different names. // To support paging you will need to add ordering to the 'Categories' query. public IQueryable<Categories> GetCategories() { return this.ObjectContext.Categories; } public void InsertCategory(Categories category) { if ((category.EntityState != EntityState.Detached)) { this.ObjectContext.ObjectStateManager.ChangeObjectState(category, EntityState.Added); } else { this.ObjectContext.Categories.AddObject(category); } } public void UpdateCategory(Categories currentCategory) { this.ObjectContext.Categories.AttachAsModified(currentCategory, this.ChangeSet.GetOriginal(currentCategory)); } public void DeleteCategory(Categories category) { if ((category.EntityState == EntityState.Detached)) { this.ObjectContext.Categories.Attach(category); } this.ObjectContext.Categories.DeleteObject(category); } } }
MainPage.xaml.cs:
using SlWCfRiaDemo.Web; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Net; using System.ServiceModel.DomainServices.Client; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace SlWCfRiaDemo { public partial class MainPage : UserControl { DomainService1 ds = new DomainService1(); public ObservableCollection<Categories> Data { get; set; } public MainPage() { InitializeComponent(); Data = new ObservableCollection<Categories>(); LoadData(); } /// <summary> /// 加载数据 /// </summary> private void LoadData() { ds.Load(ds.GetCategoriesQuery(), o => { Data.Clear(); foreach (Categories c in o.Entities) { Data.Add(c); } this.datalist.ItemsSource = Data; }, null); } /// <summary> /// 增加数据 /// </summary> /// <param name="categoryName"></param> public void Insert(string categoryName) { EntitySet<Categories> categories = ds.EntityContainer.GetEntitySet<Categories>(); Categories cate = new Categories(); cate.CategoryName = categoryName; cate.CategoryID = 12; categories.Add(cate); ds.SubmitChanges(OnInsertCompleted, cate); } private void OnInsertCompleted(SubmitOperation so) { if (!so.HasError) { Data.Insert(0, so.UserState as Categories); } } /// <summary> /// 删除数据 /// </summary> /// <param name="categories"></param> public void Delete(List<Categories> categories) { EntitySet<Categories> categoriesOriginal = ds.EntityContainer.GetEntitySet<Categories>(); foreach (Categories c in categories) { categoriesOriginal.Remove(c); } ds.SubmitChanges(OnDeleteCompleted, categories); } private void OnDeleteCompleted(SubmitOperation so) { if (!so.HasError) { List<Categories> categories = so.UserState as List<Categories>; foreach (Categories cc in categories) { Data.Remove(cc); } } } /// <summary> /// 更新数据 /// </summary> public void Update() { ds.SubmitChanges(OnUpdateCompleted, null); } private void OnUpdateCompleted(SubmitOperation so) { if (!so.HasError) { MessageBox.Show("更新成功"); } } /// <summary> /// 增加按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAdd_Click(object sender, RoutedEventArgs e) { Insert(this.txtCate.Text.Trim().ToString()); } /// <summary> /// 删除按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDele_Click_1(object sender, RoutedEventArgs e) { var collection = this.datalist.SelectedItems as System.Collections.IList; var categories = new List<Categories>(); foreach (Categories cate in collection) { categories.Add(cate); } Delete(categories); } /// <summary> /// 更新按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnUpdate_Click_1(object sender, RoutedEventArgs e) { Update(); } } }
MainPage.xaml:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="SlWCfRiaDemo.MainPage" mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="800"> <Grid x:Name="LayoutRoot" Background="White"> <TextBox x:Name="txtCate" HorizontalAlignment="Left" Height="31" Margin="138,63,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="151"/> <Button x:Name="btnAdd" Content="增 加" HorizontalAlignment="Left" Height="31" Margin="309,63,0,0" VerticalAlignment="Top" Width="82" Click="btnAdd_Click"/> <sdk:DataGrid x:Name="datalist" AutoGenerateColumns="False" HorizontalAlignment="Left" Height="276" Margin="138,133,0,0" VerticalAlignment="Top" Width="446"> <sdk:DataGrid.Columns> <sdk:DataGridTextColumn Header="类别ID" Binding="{Binding CategoryID}" IsReadOnly="True" /> <sdk:DataGridTextColumn Header="类别名称" Binding="{Binding CategoryName}" IsReadOnly="True" /> <sdk:DataGridTextColumn Header="详细描述" Binding="{Binding Description}" IsReadOnly="True" /> <sdk:DataGridTextColumn Header="图片" Binding="{Binding Picture}" /> </sdk:DataGrid.Columns> </sdk:DataGrid> <Button x:Name="btnDele" Content="删 除" HorizontalAlignment="Left" Height="31" Margin="410,63,0,0" VerticalAlignment="Top" Width="82" Click="btnDele_Click_1"/> <Button x:Name="btnUpdate" Content="更 新" HorizontalAlignment="Left" Height="31" Margin="514,63,0,0" VerticalAlignment="Top" Width="82" Click="btnUpdate_Click_1"/> </Grid> </UserControl>
更新功能没实现
本文来自朋朋朋的博客,原文地址:http://www.cnblogs.com/caok168/archive/2012/11/14/2770091.html