您的位置:首页 > 数据库

Silverlight访问数据库之Linq to SQL篇

2013-07-02 17:01 363 查看
准备工作

1)测试项目的建立

2)创建测试用数据库

由于在上一篇中我已进行详细的介绍,这里就不在赘述。

创建Linq to SQL 数据模型类

右击testDataGrid文件夹,点击菜单选项Add->New Item...。按下图进行操作,将数据库实体命名为EmployeeModel.dbml,点击Add按钮。



图1:新建Linq to SQL数据实体模型

打开Server Explorer,将Employees数据库中的Employee表拖入Linq to SQL设计器中,并且将设计器中的Employee改为Employees。(如下图)



图2:设计数据模型

接着,对EmployeeModel.dbml的属性进行设置。将Context Namespace设置为EmployeesContext,将Entity Namespace设置为EmployeesEntities。



图3:设置EmployeeModel.dbml的属性

按Ctrl+Shift+B,进行项目的编译。

建立Silverlight-enabled WCF Service数据通信服务

右击testDataGrid文件夹,点击菜单选项Add->New Item...。按下图进行操作,将WCF Service命名为EmployeeInfoWCFService.svc,点击Add按钮。



图4:新建Silverlight-enabled WCF Service

之后,将下述代码添加至EmployeeInfoWCFService.svc.cs文件中。
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using EmployeesContext;//引入数据库实体所在命名空间
using EmployeesEntities;//引入数据表实体所在命名空间

namespace testDataGrid
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class EmployeeInfoWCFService
{
//添加要调用的方法以标签[OperationContract]开头
[OperationContract]
public List<Employees> GetEmployees()
{
EmployeeModelDataContext db = new EmployeeModelDataContext();
return db.Employees.ToList();
}

}
}


 

 

按Ctrl+Shift+B,进行项目的编译。

右击SilverlightClient项目文件夹下的References,选择Add Service References...。接着,在弹出的对话框中点击Discover按钮,Services中出现刚才我们创建的EmployeeInfoWCFService.svc,点击其左边的“+”展开符号,之后出现服务搜寻,结束后将Namespace改为EmployeeWCFServiceReference。(见下图)。



图5:添加EmployeeInfoWCFService.svc的引用

 

按Ctrl+Shift+B,进行项目的编译。

这样,建立Silverlight-enabled WCF Service数据通讯服务的过程就此结束。

创建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="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot" Background="White" Width="320" Height="220">
<data:DataGrid x:Name="dgEmployee" Height="150" Margin="8,8,0,0" VerticalAlignment="Top"

HorizontalAlignment="Left" Width="304"/>
<Button x:Name="btnGetData" Height="28" HorizontalAlignment="Left" Margin="8,171,0,0"

VerticalAlignment="Top" Width="98" Content="Get Data"/>
</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.EmployeeWCFServiceReference;//引入数据服务所在命名空间

namespace SilverlightClient
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
//注册事件触发处理
this.btnGetData.Click += new RoutedEventHandler(btnGetData_Click);
}

void btnGetData_Click(object sender, RoutedEventArgs e)
{
EmployeeInfoWCFServiceClient webClient = new EmployeeInfoWCFServiceClient();
webClient.GetEmployeesAsync();//异步调用
webClient.GetEmployeesCompleted += new EventHandler<GetEmployeesCompletedEventArgs>(webClient_GetEmployeesCompleted);
}

void webClient_GetEmployeesCompleted(object sender, GetEmployeesCompletedEventArgs e)
{
dgEmployee.ItemsSource = e.Result;
}
}
}


 

最终效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息