您的位置:首页 > 其它

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

2009-08-20 23:07 1096 查看
在本教程中,主要为大家讲述如何使用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,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<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/)
版权声明:本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐