微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight访问数据库之Linq to SQL篇

本教程将使用另一常见的数据模型工具Linq to sql Class+Silverlight-enabled WCF Service来与数据库进行交互。

准备工作

1)测试项目的建立

2)创建测试用数据库

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

创建Linq to sql 数据模型类

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

clip_image002

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

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

clip_image004

图2:设计数据模型

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

clip_image006

图3:设置EmployeeModel.dbml的属性

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

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

右击testDataGrid文件夹,点击菜单选项Add->New Item...。按下图进行操作,将WCF Service命名为EmployeeInfoWCFService.svc,Tahoma; font-size:14px; line-height:25px">

clip_image008

图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();
        }

    }
}

 

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

clip_image010

图5:添加EmployeeInfoWCFService.svc的引用

这样,建立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" VerticalAlignment="Top"
         HorizontalAlignment="Left" Width="304"/>
        <Button x:Name="btnGetData" Height="28" HorizontalAlignment="Left" Margin="8,171,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,175)">GetEmployeesCompletedEventArgs e)
        {
            dgEmployee.ItemsSource = e.Result;
        }
    }
}

最终效果图:

clip_image012


转自http://www.silverlightchina.net/html/tips/2009/1210/382.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐