在本教程中,主要为大家讲述如何使用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
{
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);
}
public void Update(List
{
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);
});
}
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);
}
}
}
创建SilverlightClient界面及组件代码
MainPage.xaml文件代码:
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
int rowCount = 0;
bool isInsertOrUpdate = false;
{
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);
}
{
EmployeeInfoWCFServiceClient webClient = new EmployeeInfoWCFServiceClient();
webClient.DeleteAsync(dgEmployee.SelectedItem as Employees);
webClient.DeleteCompleted +=
new EventHandler
}
{
;
}
{
if (isInsertOrUpdate)
{
EmployeeInfoWCFServiceClient webClient = new EmployeeInfoWCFServiceClient();
webClient.InsertAsync(dgEmployee.SelectedItem as Employees);
webClient.InsertCompleted +=
new EventHandler
}
else
{
EmployeeInfoWCFServiceClient webClient = new EmployeeInfoWCFServiceClient();
ObservableCollection
foreach (object o in dgEmployee.ItemsSource)
{
updatemodel.Add(o as Employees);
}
webClient.UpdateAsync(updatemodel);
webClient.UpdateCompleted +=
new EventHandler
}
}
{
;
}
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;
}
{
EmployeeInfoWCFServiceClient webClient = new EmployeeInfoWCFServiceClient();
webClient.GetEmployeesAsync();//异步调用
webClient.GetEmployeesCompleted +=
new EventHandler
}
{
BoundData = e.Result.ToList();
dgEmployee.ItemsSource = BoundData;
rowCount = BoundData.Count;
}
}
}
最终效果图:
