silverlight+wcf+linq to sql访问数据

本文详细介绍了如何创建Silverlight应用程序,并通过添加LINQ to SQL将数据库中的表映射到类中,进一步为项目添加WCF服务。演示了如何在WCF服务类中实现获取数据的功能,并在mainpage.xaml中布局Datagrid控件和Button按钮,最后通过服务引用调用WCF服务以显示数据。

1创建silverlight应用程序

2 在解决方案右击属性添加新项 添加 linq to sql

3 把数据库中的表复制到linq  to sql类中(注意:以下截图中红色方框需要自己修改)

4 为项目添加 wcf服务

5为wcf服务类文件中添加显示数据的函数其代码如下:

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using StuEntity;
using StuContent;
using System.Collections.Generic;
using System.Text;
namespace SilverlightApplicationTest.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ServiceStuInfo
    {
        [OperationContract]
        public void DoWork()
        {
            // 在此处添加操作实现
            return;
        }


        [OperationContract]
        public List<StuMsg> GetAllMsg()
        {
            StuTestDataContext stuTest = new StuTestDataContext();
            var result = from info in stuTest.StuMsg select info;
            return result.ToList<StuMsg>();
        }
        // 在此处添加更多操作并使用 [OperationContract] 标记它们
    }
}
View Code

6 为mainpage.xaml布局datagrid控件 button按钮

7 项目添加服务引用和添加System.Data.Services.Client

8  mainpage.xaml后台代码如下:

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;
using SilverlightApplicationTest.StuServiceReference;
namespace SilverlightApplicationTest
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.btnGet.Click += new RoutedEventHandler(btnGet_Click);
        }

        private void btnGet_Click(object sender, RoutedEventArgs e)
        {
            
            ServiceStuInfoClient stu = new ServiceStuInfoClient();
            stu.GetAllMsgAsync();
            stu.GetAllMsgCompleted += new EventHandler<GetAllMsgCompletedEventArgs>(stu_GetAllMsgCompleted);
        }
          private void stu_GetAllMsgCompleted(object sender, GetAllMsgCompletedEventArgs e)
        {
            dgrid.ItemsSource = e.Result;
        }
        
    }
}
View Code

9 按F5调试运行

 

转载于:https://www.cnblogs.com/thbbsky/archive/2013/06/15/3137881.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值