国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > Access > 正文

詳解Silverlight與Access互操作的具體實現

2020-03-24 17:49:40
字體:
來源:轉載
供稿:網友
Silverlight與Access互操作是一個很基礎的問題,主要涉及到數據庫的操作。Access屬于輕量級的數據庫,應用起來還是比較方便的。51CTO編輯推薦《走向銀光 —— 一步一步學Silverlight》 在開發一些小型html' target='_blank'>應用程序時,我們就需要使用一些小巧的輕量級的數據庫,比如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文件 UserControlxmlns="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: 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="8,0,0,8"VerticalAlignment="Bottom"Width="122"TextWrapping="Wrap"FontSize="16"/ /Grid /UserControl MainPage.xaml.cs文件 usingSystem; usingSystem.Collections.Generic; usingSystem.Collections.ObjectModel; usingSystem.Linq; 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 EmployeeModel deletedID=newObservableCollection EmployeeModel ();//標記被刪除的對象 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) { List EmployeeModel updateValues=dfEmployee.ItemsSource.Cast EmployeeModel ().ToList(); ObservableCollection EmployeeModel returnValues=newObservableCollection EmployeeModel (); 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 System.ComponentModel.AsyncCompletedEventArgs (webClient_UpdateCompleted); webClient.UpdateAsync(returnValues); } elseif(updateValues.Count originalNum) { //刪除數據 EmployeesInfoWebServiceSoapClientwebClient=newEmployeesInfoWebServiceSoapClient(); webClient.DeleteCompleted+=newEventHandler System.ComponentModel.AsyncCompletedEventArgs (webClient_DeleteCompleted); webClient.DeleteAsync(deletedID); } else { //更新數據 updateValues.ForEach(x= returnValues.Add(x)); EmployeesInfoWebServiceSoapClientwebClient=newEmployeesInfoWebServiceSoapClient(); webClient.UpdateCompleted+=newEventHandler System.ComponentModel.AsyncCompletedEventArgs (webClient_UpdateCompleted); webClient.UpdateAsync(returnValues); } } voidwebClient_UpdateCompleted(objectsender,System.ComponentModel.AsyncCompletedEventArgse) { tbResult.Text="更新成功!"; } voidwebClient_DeleteCompleted(objectsender,System.ComponentModel.AsyncCompletedEventArgse) { tbResult.Text="刪除成功!"; } voidwebClient_InsertCompleted(objectsender,System.ComponentModel.AsyncCompletedEventArgse) { tbResult.Text="添加成功!"; } voidbtnGetData_Click(objectsender,RoutedEventArgse) { GetEmployees(); } voidMainPage_Loaded(objectsender,RoutedEventArgse) { GetEmployees(); } voidGetEmployees() { EmployeesInfoWebServiceSoapClientwebClient=newEmployeesInfoWebServiceSoapClient(); webClient.GetEmployeesInfoCompleted+= newEventHandler GetEmployeesInfoCompletedEventArgs (webClient_GetEmployeesInfoCompleted); webClient.GetEmployeesInfoAsync(); } voidwebClient_GetEmployeesInfoCompleted(objectsender,GetEmployeesInfoCompletedEventArgse) { originalNum=e.Result.Count;//記錄原始數據個數 dfEmployee.ItemsSource=e.Result; } } }最終效果圖html教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盐池县| 八宿县| 乐安县| 夏邑县| 大石桥市| 滁州市| 泾源县| 瑞金市| 大港区| 拜泉县| 辽源市| 句容市| 永康市| 周宁县| 汤原县| 奉化市| 永靖县| 丹东市| 合水县| 铅山县| 东乌| 娄底市| 邯郸市| 梅河口市| 兰州市| 分宜县| 安仁县| 黄石市| 湄潭县| 浪卡子县| 松原市| 曲周县| 清镇市| 巴塘县| 大理市| 松滋市| 衡阳县| 广灵县| 兴安县| 平原县| 兴安县|