热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> Silverlight>>正文内容

强大的DataGrid组件[4]_实现CURD[上]

发布时间:2009年09月14日点击数: Kinglee

返回教程目录

在本教程中,主要为大家讲述如何使用DataGrid来对后台数据库进行CURD操作。由于CURD操作是与数据库交互中最为常用的,因此掌握其使用方法就显得尤为必要。本教程将使用Linq to SQL Class + Silverlight-enabled WCF Service来实现这一过程。

准备工作

1)建立起测试项目

2)创建测试用数据库

细节详情请见强大的DataGrid组件[2]_数据交互

创建Linq to SQL 数据模型类

细节详情请见强大的DataGrid组件[3]_数据交互之Linq to SQL

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

建立的过程参见强大的DataGrid组件[3]_数据交互之Linq to SQL。然而,必须要将文件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 datagridcurd
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class EmployeeInfoWCFService
    {
        [OperationContract]
        public List GetEmployees()
        {
            EmployeeModelDataContext db = new EmployeeModelDataContext();
            return db.Employees.ToList();
        }

        
        [OperationContract]
        public void Insert(Employees employee)
        {
            EmployeeModelDataContext db = new EmployeeModelDataContext();
            string employeename = employee.EmployeeName;
            int employeeage = employee.EmployeeAge;
            string strsql = "INSERT INTO Employee(EmployeeName,EmployeeAge)
              VALUES('" + employeename + "'," + employeeage.ToString() + ")";
            db.ExecuteCommand(strsql);
        }


        [OperationContract]
        public void Update(List employee)
        {
            EmployeeModelDataContext db = new EmployeeModelDataContext();
            employee.ForEach( x =>
            {
                int employeeid = x.EmployeeID;
                string employeename = x.EmployeeName;
                int employeeage = x.EmployeeAge;
                string strsql = "UPDATE Employee SET EmployeeName='" + employeename +
                  "',EmployeeAge=" + employeeage.ToString() + "WHERE EmployeeID=" + employeeid.ToString();
                db.ExecuteCommand(strsql);
            }
);
        }


        [OperationContract]
        public void Delete(Employees employee)
        {
            EmployeeModelDataContext db = new EmployeeModelDataContext();
            int employeeid = employee.EmployeeID;
            string strsql = "DELETE FROM Employee WHERE EmployeeID="+employeeid.ToString();
            db.ExecuteCommand(strsql);
        }


        // Add more operations here and mark them with [OperationContract]
    }

}

 

创建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="320" d:DesignHeight="240">
    <Grid x:Name="LayoutRoot" Background="White" Width="320" Height="240">
        <data:DataGrid x:Name="dgEmployee" Margin="8,8,8,48" Background="#FFCEDBE8"/>
        <Button x:Name="btnGetData" Height="30" HorizontalAlignment="Left" Margin="8,0,0,8"
VerticalAlignment="Bottom" Width="64" Content="GetData"/>
        <Button x:Name="btnInsert" Height="30" HorizontalAlignment="Left" Margin="88,0,0,8"
VerticalAlignment="Bottom" Width="64" Content="Insert"/>
        <Button x:Name="btnSave" Height="30" HorizontalAlignment="Right" Margin="0,0,88,8"
VerticalAlignment="Bottom" Width="64" Content="Save"/>
        <Button x:Name="btnDelete" Height="30" HorizontalAlignment="Right" Margin="0,0,8,8"
VerticalAlignment="Bottom" Width="64" Content="Delete"/>
    </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;//引入数据服务所在命名空间
using System.Collections.ObjectModel;//引入ObservableCollection所在命名空间


namespace SilverlightClient
{
    public partial class MainPage : UserControl
    {
        List BoundData = new List();
        int rowCount = 0;
        bool isInsertOrUpdate = false;

        public MainPage()
        {
            InitializeComponent();
            this.btnGetData.Click += new RoutedEventHandler(btnGetData_Click);
            this.btnInsert.Click += new RoutedEventHandler(btnInsert_Click);
            this.btnSave.Click += new RoutedEventHandler(btnSave_Click);
            this.btnDelete.Click += new RoutedEventHandler(btnDelete_Click);
        }


        void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            EmployeeInfoWCFServiceClient webClient = new EmployeeInfoWCFServiceClient();
            webClient.DeleteAsync(dgEmployee.SelectedItem as Employees);
            webClient.DeleteCompleted +=
new EventHandler(webClient_DeleteCompleted);
        }


        void webClient_DeleteCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            ;
        }


        void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (isInsertOrUpdate)
            {
                EmployeeInfoWCFServiceClient webClient = new EmployeeInfoWCFServiceClient();
                webClient.InsertAsync(dgEmployee.SelectedItem as Employees);
                webClient.InsertCompleted +=
new EventHandler(webClient_InsertCompleted);
            }

            else
            {
                EmployeeInfoWCFServiceClient webClient = new EmployeeInfoWCFServiceClient();
                ObservableCollection updatemodel = new ObservableCollection();
                foreach (object o in dgEmployee.ItemsSource)
                {
                    updatemodel.Add(o as Employees);
                }

                webClient.UpdateAsync(updatemodel);
                webClient.UpdateCompleted +=
new EventHandler(webClient_UpdateCompleted);
            }

        }


        void webClient_UpdateCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            ;
        }

        
        void  webClient_InsertCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
             ;
        }

        
        void btnInsert_Click(object sender, RoutedEventArgs e)
        {
            Employees u = new Employees();
            BoundData.Insert(rowCount, u);
            dgEmployee.SelectedIndex = rowCount;
            dgEmployee.BeginEdit();
            dgEmployee.ItemsSource = BoundData;
            isInsertOrUpdate = true;
        }


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


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


    }

}

 

最终效果图:

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作