这里将解释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文件夹中,便于操作办理。

详解Silverlight与Access互操作的详细完成  Silverlight与Access互操 第1张

树立数据模型

EmployeeModel.cs文件(放置在服务端项目文件夹下)

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. namespacedatagridnaccessdb
  5. {
  6. publicclassEmployeeModel
  7. {
  8. publicintEmployeeID{get;set;}
  9. publicstringEmployeeName{get;set;}
  10. publicintEmployeeAge{get;set;}
  11. }
  12. }

树立服务端Web Service★

右击服务端项目文件夹,挑选Add->New Item....,按下图所示树立一个名为EmployeesInfoWebService.asmx的Web Service,作为Silverlight与Access数据库互操作的桥梁。

详解Silverlight与Access互操作的详细完成  Silverlight与Access互操 第2张

创立结束后,双击EmployeesInfoWebService.asmx翻开该文件。将里边的内容修正如下:
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Web;
  5. usingSystem.Web.Services;
  6. usingSystem.Data.OleDb;//引进该命名空间为了操作Access数据库
  7. usingSystem.Data;
  8. namespacedatagridnaccessdb
  9. {
  10. ///<summary>
  11. ///SummarydescriptionforEmployeesInfoWebService
  12. ///</summary>
  13. [WebService(Namespace="http://tempuri.org/")]
  14. [WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
  15. [System.ComponentModel.ToolboxItem(false)]
  16. //ToallowthisWebServicetobecalledfromscript,usingASP.NETAJAX,uncommentthefollowingline.
  17. //[System.Web.Script.Services.ScriptService]
  18. publicclassEmployeesInfoWebService:System.Web.Services.WebService
  19. {
  20. [WebMethod]//获取雇员信息
  21. publicList<EmployeeModel>GetEmployeesInfo()
  22. {
  23. List<EmployeeModel>returnedValue=newList<EmployeeModel>();
  24. OleDbCommandCmd=newOleDbCommand();
  25. SQLExcute("SELECT*FROMEmployee",Cmd);
  26. OleDbDataAdapterEmployeeAdapter=newOleDbDataAdapter();
  27. EmployeeAdapter.SelectCommand=Cmd;
  28. DataSetEmployeeDataSet=newDataSet();
  29. EmployeeAdapter.Fill(EmployeeDataSet);
  30. foreach(DataRowdrinEmployeeDataSet.Tables[0].Rows)
  31. {
  32. EmployeeModeltmp=newEmployeeModel();
  33. tmp.EmployeeID=Convert.ToInt32(dr[0]);
  34. tmp.EmployeeName=Convert.ToString(dr[1]);
  35. tmp.EmployeeAge=Convert.ToInt32(dr[2]);
  36. returnedValue.Add(tmp);
  37. }
  38. returnreturnedValue;
  39. }
  40. [WebMethod]//增加雇员信息
  41. publicvoidInsert(List<EmployeeModel>employee)
  42. {
  43. employee.ForEach(x=>
  44. {
  45. stringCmdText="INSERTINTOEmployee(EmployeeName,EmployeeAge)VALUES('"+x.EmployeeName+"',"+x.EmployeeAge.ToString()+")";
  46. SQLExcute(CmdText);
  47. });
  48. }
  49. [WebMethod]//更新雇员信息
  50. publicvoidUpdate(List<EmployeeModel>employee)
  51. {
  52. employee.ForEach(x=>
  53. {
  54. stringCmdText="UPDATEEmployeeSETEmployeeName='"+x.EmployeeName+"',EmployeeAge="+x.EmployeeAge.ToString();
  55. CmdText+="WHEREEmployeeID="+x.EmployeeID.ToString();
  56. SQLExcute(CmdText);
  57. });
  58. }
  59. [WebMethod]//删去雇员信息
  60. publicvoidDelete(List<EmployeeModel>employee)
  61. {
  62. employee.ForEach(x=>
  63. {
  64. stringCmdText="DELETEFROMEmployeeWHEREEmployeeID="+x.EmployeeID.ToString();
  65. SQLExcute(CmdText);
  66. });
  67. }
  68. //履行SQL指令文本,重载1
  69. privatevoidSQLExcute(stringSQLCmd)
  70. {
  71. stringConnectionString="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATASOURCE="+Server.MapPath(@"App_Data\Employees.mdb;");
  72. OleDbConnectionConn=newOleDbConnection(ConnectionString);
  73. Conn.Open();
  74. OleDbCommandCmd=newOleDbCommand();
  75. Cmd.Connection=Conn;
  76. Cmd.CommandTimeout=15;
  77. Cmd.CommandType=CommandType.Text;
  78. Cmd.CommandText=SQLCmd;
  79. Cmd.ExecuteNonQuery();
  80. Conn.Close();
  81. }
  82. //履行SQL指令文本,重载2
  83. privatevoidSQLExcute(stringSQLCmd,OleDbCommandCmd)
  84. {
  85. stringConnectionString="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATASOURCE="+Server.MapPath(@"App_Data\Employees.mdb;");
  86. OleDbConnectionConn=newOleDbConnection(ConnectionString);
  87. Conn.Open();
  88. Cmd.Connection=Conn;
  89. Cmd.CommandTimeout=15;
  90. Cmd.CommandType=CommandType.Text;
  91. Cmd.CommandText=SQLCmd;
  92. Cmd.ExecuteNonQuery();
  93. }
  94. }
  95. }

之后,在Silverlight客户端运用程序文件夹下,右击References文件夹,挑选菜单选项Add Service Reference...。如下图所示,引进方才咱们创立的Web Service(别忘了按Discover按钮进行查找)。

详解Silverlight与Access互操作的详细完成  Silverlight与Access互操 第3张

创立Silverlight客户端运用程序
  1. MainPage.xaml文件
  2. <UserControl
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. 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"
  6. d:DesignWidth="320"d:DesignHeight="240">
  7. <Gridx:Name="LayoutRoot"Width="320"Height="240"Background="White">
  8. <dataFormToolkit:DataFormx:Name="dfEmployee"Margin="8,8,8,42"/>
  9. <Buttonx:Name="btnGetData"Height="30"Margin="143,0,100,8"VerticalAlignment="Bottom"Content="GetData"Width="77"/>
  10. <Buttonx:Name="btnSaveAll"Height="30"Margin="0,0,8,8"VerticalAlignment="Bottom"Content="SaveAll"HorizontalAlignment="Right"Width="77"/>
  11. <TextBlockx:Name="tbResult"Height="30"HorizontalAlignment="Left"Margin
转载请说明出处
知优网 » 详解Silverlight与Access互操作的详细完成

发表评论

您需要后才能发表评论