强大的DataGrid组件[4]_实现CURD[上]——Silverlight学习笔记[12]

强大的DataGrid组件[4]_实现CURD[上]——Silverlight学习笔记[12],第1张

概述在本教程中,主要为大家讲述如何使用DataGrid来对后台数据库进行CURD *** 作。由于CURD *** 作是与数据库交互中最为常用的,因此掌握其使用方法就显得尤为必要。本教程将使用Linq to SQL Class + Silverlight-enabled WCF Service来实现这一过程。 准备工作 1)建立起测试项目 2)创建测试用数据库 细节详情请见强大的DataGrid组件[2]_数据交互之

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

准备工作

1)建立起测试项目

2)创建测试用数据库

细节详情请见强大的DataGrid组件[2]_数据交互之ADO.NET Entity Framework——Silverlight学习笔记[10]。

创建linq to sql 数据模型类

细节详情请见强大的DataGrid组件[3]_数据交互之Linq to SQL——Silverlight学习笔记[11]。

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

建立的过程参见强大的DataGrid组件[3]_数据交互之Linq to SQL——Silverlight学习笔记[11]。然而,必须要将文件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<Employees> 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<Employees> 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,48" Background="#FFCEDBE8"/> <button x:name="btnGetData" Height="30" HorizontalAlignment="left" margin="8,8"
VerticalAlignment="Bottom" WIDth="64" Content="GetData"/> <button x:name="btnInsert" Height="30" HorizontalAlignment="left" margin="88,8"
VerticalAlignment="Bottom" WIDth="64" Content="Insert"/> <button x:name="btnSave" Height="30" HorizontalAlignment="Right" margin="0,88,8"
VerticalAlignment="Bottom" WIDth="64" Content="Save"/> <button x:name="btnDelete" Height="30" HorizontalAlignment="Right" margin="0,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<Employees> BoundData = new List<Employees>();        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<System.ComponentModel.AsyncCompletedEventArgs>(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<System.ComponentModel.AsyncCompletedEventArgs>(webClIEnt_InsertCompleted);            }            else            {                EmployeeInfoWCFServiceClIEnt webClIEnt = new EmployeeInfoWCFServiceClIEnt();                ObservableCollection<Employees> updatemodel = new ObservableCollection<Employees>();                foreach (object o in dgEmployee.ItemsSource)                {                    updatemodel.Add(o as Employees);                 }                webClIEnt.UpdateAsync(updatemodel);                webClIEnt.UpdateCompleted += 
new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(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<GetEmployeesCompletedEventArgs>(webClIEnt_GetEmployeesCompleted);        }        voID webClIEnt_GetEmployeesCompleted(object sender,GetEmployeesCompletedEventArgs e)        {            BoundData = e.Result.ToList();            dgEmployee.ItemsSource = BoundData;            rowCount = BoundData.Count;        }    }}

最终效果图:

作者:Kinglee 
文章出处:Kinglee’s Blog ( http://www.cnblogs.com/Kinglee/) 
版权声明:本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任。 Tag标签:  Silverlight,DataGrid,Linq to SQL,WCF,CURD 总结

以上是内存溢出为你收集整理的强大的DataGrid组件[4]_实现CURD[上]——Silverlight学习笔记[12]全部内容,希望文章能够帮你解决强大的DataGrid组件[4]_实现CURD[上]——Silverlight学习笔记[12]所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1034185.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存