Silverlight RIA Service 对数据进行操作--基本测试

 

1. 新增数据实体模型和域服务类。

  服务端:MyDomainservice类,其中包含方法GetXXX,InsertXXX,UpdateXXX和DeleteXXX方法,其中XXX为表的名字。暂时没有用上。

  客户端:MyDomainContext类,其中包含属性public Entity<XXX> XXXs(需要在生成实体模型时选中单复数形式),方法EntitySet<XXX> GetXXXQuery(),用于查询数据。程序中主要用这个类。

2. 添加DataGrid控件dataGrid1:

1         <sdk:DataGrid AutoGenerateColumns="False" Height="247" HorizontalAlignment="Left" Margin="12,37,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="265" ItemsSource="{Binding ElementName=table_1DomainDataSource, Path=Data}" >
2             <sdk:DataGrid.Columns>
3                 <sdk:DataGridTextColumn Binding="{Binding id, Mode=TwoWay}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="学号" Width="Auto" />
4                 <sdk:DataGridTextColumn Binding="{Binding name, Mode=TwoWay}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="姓名" Width="Auto" />
5                 <sdk:DataGridTextColumn Binding="{Binding dept, Mode=TwoWay}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="院系" Width="Auto" />
6                 <sdk:DataGridCheckBoxColumn Binding="{Binding sex, Mode=TwoWay}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="性别" Width="Auto" />
7             </sdk:DataGrid.Columns>
8         </sdk:DataGrid>

  其中AutoGenerateColumns设置为False,ItemsSource="{Binding ElementName=table_1DomainDataSource, Path=Data}"一项是下一步后自动生成的。

3. 将数据源中的表Table_1拖到dataGrid1上:

1         <riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:Table_1, CreateList=true}" Height="0" LoadedData="table_1DomainDataSource_LoadedData" Name="table_1DomainDataSource" QueryName="GetTable_1Query" Width="0">
2             <riaControls:DomainDataSource.DomainContext>
3                 <my:MyDomainContext />
4             </riaControls:DomainDataSource.DomainContext>
5         </riaControls:DomainDataSource>

4. 在MainPage.xaml.cs中:
  (1)加入引用:

using SilverlightApplication12.Web;   
using System.ServiceModel.DomainServices.Client; 

  (2)将dataGrid1中修改的数据更新到数据表中:

1         private void buttonModify_Click(object sender, RoutedEventArgs e)
2         {
3                table_1DomainDataSource.SubmitChanges();
4         }

  (3) 插入数据:

 1         public Table_1 newRecord { get; set; }    //创建一条新记录对象
 2         private void buttonInsert_Click(object sender, RoutedEventArgs e)
 3         {
 4             newRecord = new Table_1();
 5             newRecord.id = "6";
 6             newRecord.name = "陈男";
 7             newRecord.dept = "建筑";
 8             newRecord.sex = true;
 9             MyDomainContext MyContext = (MyDomainContext)(table_1DomainDataSource.DomainContext);
10             MyContext.Table_1s.Add(newRecord);
11             table_1DomainDataSource.SubmitChanges();
12             dataGrid1.ItemsSource = MyContext.Table_1s;  //刷新dataGrid1的数据
13         }

  (4)删除数据:

1         private void buttonDelete_Click(object sender, RoutedEventArgs e)
2         {
3             MyDomainContext MyContext = (MyDomainContext)(table_1DomainDataSource.DomainContext);
4             MyContext.Table_1s.Remove(dataGrid1.SelectedItem as Table_1);
5             table_1DomainDataSource.SubmitChanges();
6         }

以上代码都没有考虑异常情况!

posted @ 2012-07-25 15:01  ajeeky  阅读(1503)  评论(0编辑  收藏  举报