zoukankan      html  css  js  c++  java
  • silverlight+wcf+linq to sql访问数据

    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调试运行

  • 相关阅读:
    union 和 union all的区别
    JDBC中PreparedStatement相比Statement的好处
    25个经典的Spring面试问答
    MySQL 事务
    漫谈Linux下的音频问题(转)
    监控 Linux 性能的 18 个命令行工具(转)
    在终端中用默认程序打开文件(转)
    【转】使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
    Getting Started with Amazon EC2 (1 year free AWS VPS web hosting)
    压缩解压命令小结
  • 原文地址:https://www.cnblogs.com/thbbsky/p/3137881.html
Copyright © 2011-2022 走看看