5 Silverlight访问数据库之Access 数据库文档格式.docx

上传人:b****6 文档编号:18364323 上传时间:2022-12-15 格式:DOCX 页数:12 大小:78.34KB
下载 相关 举报
5 Silverlight访问数据库之Access 数据库文档格式.docx_第1页
第1页 / 共12页
5 Silverlight访问数据库之Access 数据库文档格式.docx_第2页
第2页 / 共12页
5 Silverlight访问数据库之Access 数据库文档格式.docx_第3页
第3页 / 共12页
5 Silverlight访问数据库之Access 数据库文档格式.docx_第4页
第4页 / 共12页
5 Silverlight访问数据库之Access 数据库文档格式.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

5 Silverlight访问数据库之Access 数据库文档格式.docx

《5 Silverlight访问数据库之Access 数据库文档格式.docx》由会员分享,可在线阅读,更多相关《5 Silverlight访问数据库之Access 数据库文档格式.docx(12页珍藏版)》请在冰豆网上搜索。

5 Silverlight访问数据库之Access 数据库文档格式.docx

usingSystem.Linq;

namespacedatagridnaccessdb

{

publicclassEmployeeModel

{

publicintEmployeeID{get;

set;

}

publicstringEmployeeName{get;

publicintEmployeeAge{get;

}

建立服务端WebService★

右击服务端项目文件夹,选择Add->

NewItem....,按下图所示建立一个名为EmployeesInfoWebService.asmx的WebService,作为Silverlight与Access数据库互操作的桥梁。

创建完毕后,双击EmployeesInfoWebService.asmx打开该文件。

将里面的内容修改如下:

usingSystem.Web;

usingSystem.Web.Services;

usingSystem.Data.OleDb;

//引入该命名空间为了操作Access数据库

usingSystem.Data;

///<

summary>

///SummarydescriptionforEmployeesInfoWebService

/summary>

[WebService(Namespace="

http:

//tempuri.org/"

)]

[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]

[System.ComponentModel.ToolboxItem(false)]

//ToallowthisWebServicetobecalledfromscript,usingASP.NETAJAX,uncommentthefollowingline.

//[System.Web.Script.Services.ScriptService]

publicclassEmployeesInfoWebService:

System.Web.Services.WebService

[WebMethod]//获取雇员信息

publicList<

EmployeeModel>

GetEmployeesInfo()

List<

returnedValue=newList<

();

OleDbCommandCmd=newOleDbCommand();

SQLExcute("

SELECT*FROMEmployee"

Cmd);

OleDbDataAdapterEmployeeAdapter=newOleDbDataAdapter();

EmployeeAdapter.SelectCommand=Cmd;

DataSetEmployeeDataSet=newDataSet();

EmployeeAdapter.Fill(EmployeeDataSet);

foreach(DataRowdrinEmployeeDataSet.Tables[0].Rows)

EmployeeModeltmp=newEmployeeModel();

tmp.EmployeeID=Convert.ToInt32(dr[0]);

tmp.EmployeeName=Convert.ToString(dr[1]);

tmp.EmployeeAge=Convert.ToInt32(dr[2]);

returnedValue.Add(tmp);

returnreturnedValue;

[WebMethod]//添加雇员信息

publicvoidInsert(List<

employee)

employee.ForEach(x=>

stringCmdText="

INSERTINTOEmployee(EmployeeName,EmployeeAge)VALUES('

"

+x.EmployeeName+"

'

"

+x.EmployeeAge.ToString()+"

)"

;

SQLExcute(CmdText);

});

[WebMethod]//更新雇员信息

publicvoidUpdate(List<

employee.ForEach(x=>

UPDATEEmployeeSETEmployeeName='

EmployeeAge="

+x.EmployeeAge.ToString();

CmdText+="

WHEREEmployeeID="

+x.EmployeeID.ToString();

[WebMethod]//删除雇员信息

publicvoidDelete(List<

DELETEFROMEmployeeWHEREEmployeeID="

//执行SQL命令文本,重载1

privatevoidSQLExcute(stringSQLCmd)

stringConnectionString="

PROVIDER=Microsoft.Jet.OLEDB.4.0;

DATASOURCE="

+Server.MapPath(@"

App_Data\Employees.mdb;

);

OleDbConnectionConn=newOleDbConnection(ConnectionString);

Conn.Open();

Cmd.Connection=Conn;

Cmd.CommandTimeout=15;

Cmd.CommandType=CommandType.Text;

Cmd.CommandText=SQLCmd;

Cmd.ExecuteNonQuery();

Conn.Close();

//执行SQL命令文本,重载2

privatevoidSQLExcute(stringSQLCmd,OleDbCommandCmd)

之后,在Silverlight客户端应用程序文件夹下,右击References文件夹,选择菜单选项AddServiceReference...。

如下图所示,引入刚才我们创建的WebService(别忘了按Discover按钮进行查找)。

创建Silverlight客户端应用程序

MainPage.xaml文件

<

UserControl

xmlns="

xmlns:

x="

d="

xmlns:

mc="

//schemas.openxmlformats.org/markup-compatibility/2006"

mc:

Ignorable="

d"

data="

clr-namespace:

System.Windows.Controls;

assembly=System.Windows.Controls.Data"

dataFormToolkit="

assembly=System.Windows.Controls.Data.DataForm.Toolkit"

x:

Class="

SilverlightClient.MainPage"

d:

DesignWidth="

320"

d:

DesignHeight="

240"

>

<

Gridx:

Name="

LayoutRoot"

Width="

Height="

Background="

White"

<

dataFormToolkit:

DataFormx:

dfEmployee"

Margin="

8,8,8,42"

/>

Buttonx:

btnGetData"

30"

143,0,100,8"

VerticalAlignment="

Bottom"

Content="

GetData"

77"

btnSaveAll"

0,0,8,8"

SaveAll"

HorizontalAlignment="

Right"

TextBlockx:

tbResult"

Left"

8,0,0,8"

122"

TextWrapping="

Wrap"

FontSize="

16"

/Grid>

/UserControl>

MainPage.xaml.cs文件

usingSystem.Collections.ObjectModel;

usingSystem.Net;

usingSystem.Windows;

usingSystem.Windows.Controls;

usingSystem.Windows.Documents;

usingSystem.Windows.Input;

usingSystem.Windows.Media;

usingSystem.Windows.Media.Animation;

usingSystem.Windows.Shapes;

usingSystem.Xml;

usingSystem.Xml.Linq;

usingSystem.Windows.Browser;

usingSilverlightClient.EmployeesInfoServiceReference;

namespaceSilverlightClient

publicpartialclassMainPage:

UserControl

intoriginalNum;

//记录初始时的Employee表中的数据总数

ObservableCollection<

deletedID=newObservableCollection<

//标记被删除的对象

publicMainPage()

InitializeComponent();

this.Loaded+=newRoutedEventHandler(MainPage_Loaded);

this.btnGetData.Click+=newRoutedEventHandler(btnGetData_Click);

this.btnSaveAll.Click+=newRoutedEventHandler(btnSaveAll_Click);

this.dfEmployee.DeletingItem+=newEventHandler<

System.ComponentModel.CancelEventArgs>

(dfEmployee_DeletingItem);

voiddfEmployee_DeletingItem(objectsender,System.ComponentModel.CancelEventArgse)

deletedID.Add(dfEmployee.CurrentItemasEmployeeModel);

//正在删除时,将被删除对象进行标记,以便传给服务端真正删除。

voidbtnSaveAll_Click(objectsender,RoutedEventArgse)

updateValues=dfEmployee.ItemsSource.Cast<

().ToList();

returnValues=newObservableCollection<

if(updateValues.Count>

originalNum)

//添加数据

for(inti=originalNum;

i<

=updateValues.Count-1;

i++)

returnValues.Add(updateValues.ToArray()[i]);

EmployeesInfoWebServiceSoapClientwebClient=newEmployeesInfoWebServiceSoapClient();

webClient.InsertCompleted+=newEventHandler<

System.ComponentModel.AsyncCompletedEventArgs>

(webClient_InsertCompleted);

webClient.InsertAsync(returnValues);

//必须考虑数据集中既有添加又有更新的情况

returnValues.Clear();

updateValues.ForEach(x=>

returnValues.Add(x));

webClient.UpdateCompleted+=newEventHandler<

(webClient_UpdateCompleted);

webClient.UpdateAsync(returnValues);

elseif(updateValues.Count<

//删除数据

webClient.DeleteCompleted+=newEventHandler<

(webClient_DeleteCompleted);

webClient.DeleteAsync(deletedID);

else

//更新数据

voidwebClient_UpdateCompleted(objectsender,System.ComponentModel.AsyncCompletedEventArgse)

tbResult.Text="

更新成功!

GetEmployees();

//更新originalNum防止数据的重复插入,感谢园友紫色永恒的及时指出!

voidwebClient_DeleteCompleted(objectsender,System.ComponentModel.AsyncCompletedEventArgse)

删除成功!

voidwebClient_InsertCompleted(objectsender,System.ComponentModel.AsyncCompletedEventArgse)

添加成功!

voidbtnGetData_Click(objectsender,RoutedEventArgse)

GetEmployees();

voidMainPage_Loaded(objectsender,RoutedEventArgse)

voidGetEmployees()

webClient.GetEmployeesInfoCompleted+=

newEventHandler<

GetEmployeesInfoCompletedEventArgs>

(webClient_GetEmployeesInfoCompleted);

webClient.GetEmployeesInfoAsync();

voidwebClient_GetEmployeesInfoCompleted(objectsender,GetEmployeesInfoCompletedEventArgse)

originalNum=e.Result.Count;

//记录原始数据个数

dfEmployee.ItemsSource=e.Result;

最终效果图

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 高等教育 > 其它

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1