大佬教程收集整理的这篇文章主要介绍了Silverlight访问数据库之ADO.NET Entity Framework篇,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在今天的教程中,我将为大家介绍怎样使用ADO.NET Entity Framework来与数据库进行基本的交互。
概览
为了能够使事情变得简单易做,这里我先为大家讲解怎样从数据库检索数据并返回至DataGrid。这个例子将为我们展示如下的功能:
这里,如果用的测试数据的量较大的话可以观测到明显的延时现象(这就是异步操作带来的效果)。
简单的准备工作
为了能使ADO.NET Entity Framework正常工作,请为你的VS2008打上SP1服务包。
附SP1下载地址:
中文版:
http://www.microsoft.com/downloadS/details.aspx?familyid=27673C47-B3B5-4C67-BD99-84E525B5CE61&displaylang=zh-cn
英文版:
http://www.microsoft.com/downloadS/details.aspx?familyid=27673C47-B3B5-4C67-BD99-84E525B5CE61&displaylang=en
创建项目
点击菜单File->New->Project...,打开New Project对话框。在Project types中选择Web,在右侧的Templates中选择ASP.NET Web Application,输入项目名为testDataGrid,点击OK按钮,完成创建。(见图1)
图1:创建ASP.NET Web Application
接着,右击Solution Explorer中Solution 'testDataGrid'根文件夹,依次点击菜单选项Add->New Project...。在Project types中选择Silverlight,在右侧的Templates中选择Silverlight Application,输入项目名为SilverLightclient,点击OK按钮,完成创建。(见图2和图3)
@H_403_57@
图2:新建Silverlight项目
图3:添加新项目
在弹出的对话框中,按下图进行设置,然后点击OK按钮。
图4:新建Silverlight应用程序
创建数据库
为了加快实验速度,我们使用sql Server Express 2008作为后台数据库。
按如下步骤建立数据库及数据表:
1)右击testDataGrid文件夹下的App_Data文件夹,依次点击菜单选项Add->New Item...。在弹出的对话框中,输入数据库名为employees.mdf,点击Add按钮。
双击employees.mdf,打开Server Explorer窗口。按下两图所示添加新数据表,将该表命名为employee。
图6:添加新数据表
图7:employee表设置
输入一些测试数据。
图8:输入一些测试数据
创建ADO.NET Entity数据库实体模型
右击testDataGrid文件夹,点击菜单选项Add->New Item...。按下图进行操作,将数据库实体命名为employeeModel.edmx,点击Add按钮。
图9:新建实体模型
在弹出对话框中选Generate from database,点击Next按钮。按下图进行操作,直到Finish为止。
图11:选择数据链接
图12:选择数据库对象
这样数据模型就建立完毕。
建立ADO.NET Data service数据通讯服务
右击testDataGrid文件夹,依次点击Add->New Item...。然后按下图操作,将ADO.NET Data service服务命名为employeeInfoservice.svc。
图13:创建ADO.NET Data service
修改employeeInfoservice.svc.cs代码如下:
using System; using System.Collections.Generic; using System.Data.services; using System.Linq; using System.serviceModel.Web; using System.Web; namespace testDataGrid { public class employeeInfoservice : Dataservice<employeesEntities> { public static void Initializeservice(IDataserviceConfiguration config) { config.SetEntitySetAccessRule("*",EntitySetrights.All); config.SetserviceOperationAccessRule(serviceOperationRights.All); } } }
按Ctrl+Shift+B进行整个项目的编译(非常重要,不然数据服务引用会出错!)
右击刚才创建的employeeInfoservice.svc,选择菜单选项View in Browser(如下图),配置成功的话会出现下图所示的页面。
图14:查看Web service配置
图15:配置成功时出现的页面
右击SilverLightclient项目文件夹下的References,选择Add service References...。接着,在弹出的对话框中点击Discover按钮,services中出现刚才我们创建的employeeInfoservice.svc,点击其左边的“+”展开符号,之后出现服务搜寻,结束后将Namespace改为employeeserviceReference。(见下图)。
图16:添加服务引用
这样,建立ADO.NET Data service数据通讯服务的过程就此结束。
<UserControl xmlns="http://scheR_585_11845@as.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://scheR_585_11845@as.microsoft.com/winfx/2006/xaml" xmlns:d="http://scheR_585_11845@as.microsoft.com/expression/blend/2008" xmlns:@H_640_292@mc="http://scheR_585_11845@as.openxmlformats.org/markup-compatibility/2006" @H_640_292@mc:Ignorable="d" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverLightclient.MainPage" d:DesignWidth="640" d:DesignHeight="480"> <Grid x:Name="LayoutRoot" BACkground="White" Width="320" Height="220"> <data:DataGrid x:Name="dgemployee" Height="150" @H_640_292@margin="8,8,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="304"/> <Button x:Name="btnGetData" Height="28" HorizontalAlignment="Left" @H_640_292@margin="8,171,0" VerticalAlignment="Top" Width="98" Content="Get Data"/> </Grid> </UserControl>
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 @H_854_227@mainPage : UserControl { public @H_668_22@mainPage() { InitializeComponent(); //注册事件触发处理 this.btnGetData.Click += new RoutedEventHandler(btnGetData_Click); } void btnGetData_Click(object sender,175)">RoutedEventArgs E) { employeesEntities proxy = new employeesEntities(new Uri("employeeInfoservice.svc",175)">UriKind.RelativE)); var query = from c in proxy.employee SELEct c; DataserviceQuery<employee> userQuery = (employee>)query; userQuery.beginExecute(new AsyncCallBACk(OnLoadCompletE),query);//异步调用 } void OnLoadComplete(IAsyncResult result) { employee> query = (employee>)result.AsyncState; dgemployee.Itemssource = query.EndExecute(result).ToList();//将最终结果传给DataGrid } } }
最后,按F5键进行编译测试。
最终效果图
转自http://www.silverLightchina.net/html/tips/2009/1210/381.html
以上是大佬教程为你收集整理的Silverlight访问数据库之ADO.NET Entity Framework篇全部内容,希望文章能够帮你解决Silverlight访问数据库之ADO.NET Entity Framework篇所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。