这里将解释Silverlight与Access互操作的具体实现,Silverlight与SQL Server或SQL Server Express的互操作,已成为我们常见的开发Siverlight应用程序的一种模式。
Silverlight与Access互操作是一个很根底的问题,首要涉及到数据库的操作。Access归于轻量级的数据库,运用起来仍是比较便利的。51CTO修正引荐《走向银光 —— 一步一步学Silverlight》
在开发一些小型运用程序时,咱们就需要运用一些细巧的轻量级的数据库,比方Access数据库。因为Visual Studio中并没有直接供给Silverlight与Access互操作的系列办法。所以本文就将为咱们介绍怎么让Silverlight运用Access作为后台数据库。
准备工作
1)树立起测验项目
细节概况请见强壮的DataGrid组件[2]_数据交互之ADO.NET Entity Framework——Silverlight学习笔记[10]。
2)创立测验用数据库
如下图所示,创立一个名为Employees.mdb的Access数据库,树立数据表名称为Employee。将该数据库置于作为服务端的项目文件夹下的App_Data文件夹中,便于操作办理。
树立数据模型
EmployeeModel.cs文件(放置在服务端项目文件夹下)
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- namespacedatagridnaccessdb
- {
- publicclassEmployeeModel
- {
- publicintEmployeeID{get;set;}
- publicstringEmployeeName{get;set;}
- publicintEmployeeAge{get;set;}
- }
- }
树立服务端Web Service★
右击服务端项目文件夹,挑选Add->New Item....,按下图所示树立一个名为EmployeesInfoWebService.asmx的Web Service,作为Silverlight与Access数据库互操作的桥梁。
创立结束后,双击EmployeesInfoWebService.asmx翻开该文件。将里边的内容修正如下:
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Web;
- usingSystem.Web.Services;
- usingSystem.Data.OleDb;//引进该命名空间为了操作Access数据库
- usingSystem.Data;
- namespacedatagridnaccessdb
- {
- ///<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<EmployeeModel>returnedValue=newList<EmployeeModel>();
- 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<EmployeeModel>employee)
- {
- employee.ForEach(x=>
- {
- stringCmdText="INSERTINTOEmployee(EmployeeName,EmployeeAge)VALUES('"+x.EmployeeName+"',"+x.EmployeeAge.ToString()+")";
- SQLExcute(CmdText);
- });
- }
- [WebMethod]//更新雇员信息
- publicvoidUpdate(List<EmployeeModel>employee)
- {
- employee.ForEach(x=>
- {
- stringCmdText="UPDATEEmployeeSETEmployeeName='"+x.EmployeeName+"',EmployeeAge="+x.EmployeeAge.ToString();
- CmdText+="WHEREEmployeeID="+x.EmployeeID.ToString();
- SQLExcute(CmdText);
- });
- }
- [WebMethod]//删去雇员信息
- publicvoidDelete(List<EmployeeModel>employee)
- {
- employee.ForEach(x=>
- {
- stringCmdText="DELETEFROMEmployeeWHEREEmployeeID="+x.EmployeeID.ToString();
- SQLExcute(CmdText);
- });
- }
- //履行SQL指令文本,重载1
- privatevoidSQLExcute(stringSQLCmd)
- {
- stringConnectionString="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATASOURCE="+Server.MapPath(@"App_Data\Employees.mdb;");
- OleDbConnectionConn=newOleDbConnection(ConnectionString);
- Conn.Open();
- OleDbCommandCmd=newOleDbCommand();
- Cmd.Connection=Conn;
- Cmd.CommandTimeout=15;
- Cmd.CommandType=CommandType.Text;
- Cmd.CommandText=SQLCmd;
- Cmd.ExecuteNonQuery();
- Conn.Close();
- }
- //履行SQL指令文本,重载2
- privatevoidSQLExcute(stringSQLCmd,OleDbCommandCmd)
- {
- 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();
- }
- }
- }
之后,在Silverlight客户端运用程序文件夹下,右击References文件夹,挑选菜单选项Add Service Reference...。如下图所示,引进方才咱们创立的Web Service(别忘了按Discover按钮进行查找)。
创立Silverlight客户端运用程序
- 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"xmlns:dataFormToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"x:Class="SilverlightClient.MainPage"
- d:DesignWidth="320"d:DesignHeight="240">
- <Gridx:Name="LayoutRoot"Width="320"Height="240"Background="White">
- <dataFormToolkit:DataFormx:Name="dfEmployee"Margin="8,8,8,42"/>
- <Buttonx:Name="btnGetData"Height="30"Margin="143,0,100,8"VerticalAlignment="Bottom"Content="GetData"Width="77"/>
- <Buttonx:Name="btnSaveAll"Height="30"Margin="0,0,8,8"VerticalAlignment="Bottom"Content="SaveAll"HorizontalAlignment="Right"Width="77"/>
- <TextBlockx:Name="tbResult"Height="30"HorizontalAlignment="Left"Margin
转载请说明出处
知优网 » 详解Silverlight与Access互操作的详细完成
知优网 » 详解Silverlight与Access互操作的详细完成