This tutorial describes how to use the DataGrid to perform curd operations on the background database. Because the curd operation is the most commonly used in database interaction, it is especially necessary to master its usage. In this tutorial, we will use LINQ to SQL class + Silverlight-enabled WCF Service to implement this process.
Preparations
1) create a test project
2) create a Test Database
For details, see the powerful DataGrid component [2] _ ADO. NET Entity Framework -- Silverlight Study Notes [10].
Create a LINQ to SQL data model class
For details, see the powerful DataGrid component [3] _ Data Interaction-to-SQL-Silverlight Study Notes [11].
Establish Silverlight-enabled WCF Service Data Communication Service
For the creation process, see the powerful DataGrid component [3] _ Data Interaction-to-SQL-Silverlight Study Notes [11]. However, you must change the file employeeinfowcfservice. SVC. CS to the following:
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 = maid; 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] }}
Create the silverlightclient interface and componentsCode
Mainpage. XAMLFile Code:
< 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" 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"
Verticalalignment = "Bottom" Width = "64" Content = "Delete"/> </ Grid > </ Usercontrol >
Mainpage. XAML. CSFile Code:
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; // Introduce the system. Data. Services. Client namespace Using Silverlightclient. employeewcfservicereference; // Introduce the namespace of the Data Service Using System. Collections. objectmodel; // Introduce the namespace of the 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 OIn 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 (); // Asynchronous call WebClient. getemployeescompleted + =
NewEventhandler<Getemployeescompletedeventargs> (Webclient_getemployeescompleted );}VoidWebclient_getemployeescompleted (ObjectSender,GetemployeescompletedeventargsE) {bounddata = E. Result. tolist (); dgemployee. itemssource = bounddata; rowcount = bounddata. Count ;}}}
Finally:
Author: kinglee
Article Source: kinglee's blog (http://www.cnblogs.com/Kinglee)
Copyright: The copyright of this article is shared by the author and the blog. The detailed link of this article must be noted during reprinting; otherwise, the author will be held legally liable.