ADO.NET Data Service
[2] 2.添加ADO.NET数据服务
[3] 3.NET客启访问DataService
[4] 4.Silverlight客启访问DataService
[5] 5.URL访问
[6] 6.WEB方法
Silverlight客启访问DataService
Silverlight 2.0 可以利用两种方法与DataServices交互:
- HttpWebRequest 与 HttpWebResponse
- System.Data.Services.Client.DataServiceContext
Silverlight 2.0 是使用异步方法来连接远程数据源。
LINQ 的 select ,要用 BeginExecute() 和 EndExecute()
LINQ 的 SaveChanges(),要用 BeginSaveChanges() 和 EndSaveChanges()
引用服务
xaml页面
<StackPanel Loaded="StackPanel_Loaded"> <data:DataGrid Name="dg">data:DataGrid> <Button Content="DataServiceContext方式加载数据" Width="200" Click="loadData_Click" /> <Button Content="WebClient方式加载数据" Width="200" Click="loadData2_Click" /> <Button Content="添加" Width="200" Click="add_Click" /> <Button Content="修改" Width="200" Click="edit_Click" /> <Button Content="删除" Width="200" Click="del_Click" /> StackPanel> |
myService.myDBEntities server; private void StackPanel_Loaded(object sender, RoutedEventArgs e) { Uri url = new Uri("http://localhost:1468/myWebDataService.svc"); server = new myService.myDBEntities(url); } void saveChanges_completed(IAsyncResult obj) {
} |
查询(WebClient方式)
//WebClient方式加载数据 private void loadData2_Click(object sender, RoutedEventArgs e) { Uri uri = new Uri("http://localhost:1468/myWebDataService.svc/tabA"); WebClient client = new WebClient(); client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_completed); client.OpenReadAsync(uri); }
void client_completed(object sender, OpenReadCompletedEventArgs e) { if (e.Error == null) { XmlReader reader = XmlReader.Create(e.Result); XDocument ls = XDocument.Load(reader); XNamespace xmlns = "http://www.w3.org/2005/Atom"; XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices"; XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
var list = from x in ls.Descendants(xmlns + "entry") select new myService.tabA { a= x.Descendants(d + "a").First().Value, b = x.Descendants(d + "b").First().Value, c = x.Descendants(d + "c").First().Value }; dg.ItemsSource = list; } } |
查询
//加载 private void loadData_Click(object sender, RoutedEventArgs e) { Uri urlE = new Uri("http://localhost:1468/myWebDataService.svc/tabX"); server.BeginExecutetabX>(urlE, new AsyncCallback(load_completed), null); }
void load_completed(IAsyncResult obj) { var tp = server.EndExecutetabX>(obj); this.dg.ItemsSource = tp.ToList(); } |
添加
//添加 private void add_Click(object sender, RoutedEventArgs e) { server.AddTotabX(new myService.tabX() { x="007",z="sss",y="sss" }); server.BeginSaveChanges(new AsyncCallback(saveChanges_completed), null); } |
编辑
//编辑 private void edit_Click(object sender, RoutedEventArgs e) { Uri urlE = new Uri("http://localhost:1468/myWebDataService.svc/tabX"); server.BeginExecutetabX>(urlE, new AsyncCallback(begin_edit), null); } void begin_edit(IAsyncResult obj) { var tp = server.EndExecutetabX>(obj).Where(p => p.x == "007 ").First(); tp.y = "hello"; server.UpdateObject(tp); server.BeginSaveChanges(new AsyncCallback(saveChanges_completed), null); } |
删除
//删除 private void del_Click(object sender, RoutedEventArgs e) { Uri urlE = new Uri("http://localhost:1468/myWebDataService.svc/tabX"); server.BeginExecutetabX>(urlE, new AsyncCallback(begin_del), null); }
void begin_del(IAsyncResult obj) { var tp = server.EndExecutetabX>(obj).Where(p => p.x == "007 ").First(); server.DeleteObject(tp); server.BeginSaveChanges(new AsyncCallback(saveChanges_completed), null);
} |