返回教程目录
在上一篇中,我们了解到怎样使用Linq to SQL+Silverlight-enabled WCF Web Service与DataGrid进行CURD操作。本教程将用另一种常用的数据通讯方式ADO.NET Entity Framework+ADO.NET Data Service来实现与DataGrid进行CURD操作的过程,大家可以比较一下,两者的服务端与客户端操作方式之间的差别。
准备工作
1)建立起测试项目
2)创建测试用数据库
细节详情请见强大的DataGrid组件[2]_数据交互。
创建ADO.NET Entity数据库实体模型
详情请见强大的DataGrid组件[2]_数据交互。
建立ADO.NET Data Service数据通讯服务
具体过程请见强大的DataGrid组件[2]_数据交互,这里不再赘述。
通过对比可以发现,ADO.NET Entity Framework+ADO.NET Data Service在服务端的配置相对于Linq to SQL+Silverlight-enabled WCF Web Service而言更为方便;而在客户端的配置方面则恰好相反,个人感觉ADO.NET Entity Framework+ADO.NET Data Service在操作方面较为复杂。
有关CURD过程的讲解在代码的注释部分中具体给出。
创建SilverlightClient界面及组件代码
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"
mc:Ignorable="d" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightClient.MainPage"
d:DesignWidth="320" d:DesignHeight="240">
<Grid x:Name="LayoutRoot" Background="White" Width="320" Height="240">
<data:DataGrid x:Name="dgEmployee" Margin="8,8,8,48" Background="#FFCEDBE8"/>
<Button x:Name="btnGetData" Height="30" HorizontalAlignment="Left" Margin="8,0,0,8" VerticalAlignment="Bottom" Width="64" Content="GetData"/>
<Button x:Name="btnInsert" Height="30" HorizontalAlignment="Left" Margin="88,0,0,8" VerticalAlignment="Bottom" Width="64" Content="Insert"/>
<Button x:Name="btnSave" Height="30" HorizontalAlignment="Right" Margin="0,0,88,8" VerticalAlignment="Bottom" Width="64" Content="Save"/>
<Button x:Name="btnDelete" Height="30" HorizontalAlignment="Right" Margin="0,0,8,8" VerticalAlignment="Bottom" Width="64" Content="Delete"/>
</Grid>
</UserControl>
MainPage.xaml.cs文件代码:

using ...System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

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;

using System.Data.Services.Client;//引入System.Data.Services.Client命名空间

using SilverlightClient.EmployeeServiceReference;//引入数据服务所在命名空间
namespace SilverlightClient

...{

public partial class MainPage : UserControl


...{

List BoundData = new List();//用于绑定的中间件

int rowCount = 0;//Employee表中数据个数

bool inEdit;//判断DataGrid是否处于编辑状态

EmployeesEntities proxy;//数据实体


public MainPage()


...{

InitializeComponent();

//注册事件触发处理

this.btnGetData.Click += new RoutedEventHandler(btnGetData_Click);

this.btnInsert.Click += new RoutedEventHandler(btnInsert_Click);

this.btnSave.Click += new RoutedEventHandler(btnSave_Click);

this.btnDelete.Click += new RoutedEventHandler(btnDelete_Click);

this.dgEmployee.CellEditEnded += new EventHandler(dgEmployee_CellEditEnded);

this.dgEmployee.BeginningEdit += new EventHandler(dgEmployee_BeginningEdit);

}


void dgEmployee_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)


...{

inEdit = true;

}


void dgEmployee_CellEditEnded(object sender, DataGridCellEditEndedEventArgs e)


...{

inEdit = false;

try


...{

proxy.AttachTo("Employee", e.Row);//附着数据行

}

catch ...{ }

proxy.UpdateObject(e.Row.DataContext);//更新数据行

}


void btnDelete_Click(object sender, RoutedEventArgs e)


...{

if (dgEmployee.SelectedItem != null)//所选行非空


...{

try


...{

proxy.DeleteObject(dgEmployee.SelectedItem);//删除所选数据行

BoundData.Remove((Employee)dgEmployee.SelectedItem);//绑定中间件移除数据项

proxy.BeginSaveChanges(SaveChangesOptions.Batch, (asyncResult) =>


...{

try


...{

proxy.EndSaveChanges(asyncResult);

}

catch ...{ }

}, null);//异步操作

}

catch ...{ }

}

}


void btnSave_Click(object sender, RoutedEventArgs e)


...{

if (!inEdit)


...{

proxy.BeginSaveChanges(SaveChangesOptions.Batch, (asyncResult) =>


...{

try


...{

proxy.EndSaveChanges(asyncResult);

}

catch ...{ }

}, null);

}//异步操作

}


void btnInsert_Click(object sender, RoutedEventArgs e)


...{

Employee u = new Employee();

BoundData.Insert(rowCount, u);

dgEmployee.SelectedIndex = rowCount;

dgEmployee.BeginEdit();

proxy.AddObject("Employee", u);

}


void btnGetData_Click(object sender, RoutedEventArgs e)


...{

proxy = new EmployeesEntities(new Uri("EmployeeInfoService.svc", UriKind.Relative));

var query = from c in proxy.Employee select c;

DataServiceQuery userQuery = (DataServiceQuery)query;

userQuery.BeginExecute(new AsyncCallback(OnLoadComplete), query);//异步调用

}


void OnLoadComplete(IAsyncResult result)


...{

DataServiceQuery query = (DataServiceQuery)result.AsyncState;

BoundData = query.EndExecute(result).ToList();

dgEmployee.ItemsSource = BoundData;//将最终结果传给DataGrid

rowCount = BoundData.Count;

}

}

}
最终效果图:
