本篇內(nèi)容介紹了“Silverlight與Access互操作的實(shí)現(xiàn)方法”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
在清原等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作按需開(kāi)發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),營(yíng)銷型網(wǎng)站,外貿(mào)營(yíng)銷網(wǎng)站建設(shè),清原網(wǎng)站建設(shè)費(fèi)用合理。
Silverlight與Access互操作是一個(gè)很基礎(chǔ)的問(wèn)題,主要涉及到數(shù)據(jù)庫(kù)的操作。Access屬于輕量級(jí)的數(shù)據(jù)庫(kù),應(yīng)用起來(lái)還是比較方便的。
在開(kāi)發(fā)一些小型應(yīng)用程序時(shí),我們就需要使用一些小巧的輕量級(jí)的數(shù)據(jù)庫(kù),比如Access數(shù)據(jù)庫(kù)。由于Visual Studio中并沒(méi)有直接提供Silverlight與Access互操作的系列方法。于是本文就將為大家介紹如何讓Silverlight使用Access作為后臺(tái)數(shù)據(jù)庫(kù)。
準(zhǔn)備工作
1)建立起測(cè)試項(xiàng)目
細(xì)節(jié)詳情請(qǐng)見(jiàn)強(qiáng)大的DataGrid組件[2]_數(shù)據(jù)交互之ADO.NET Entity Framework——Silverlight學(xué)習(xí)筆記[10]。
2)創(chuàng)建測(cè)試用數(shù)據(jù)庫(kù)
如下圖所示,創(chuàng)建一個(gè)名為Employees.mdb的Access數(shù)據(jù)庫(kù),建立數(shù)據(jù)表名稱為Employee。將該數(shù)據(jù)庫(kù)置于作為服務(wù)端的項(xiàng)目文件夾下的App_Data文件夾中,便于操作管理。

建立數(shù)據(jù)模型
EmployeeModel.cs文件(放置在服務(wù)端項(xiàng)目文件夾下)
using System; using System.Collections.Generic; using System.Linq; namespace datagridnaccessdb { public class EmployeeModel { public int EmployeeID { get; set; } public string EmployeeName { get; set; } public int EmployeeAge { get; set; } } }建立服務(wù)端Web Service★
右擊服務(wù)端項(xiàng)目文件夾,選擇Add->New Item....,按下圖所示建立一個(gè)名為EmployeesInfoWebService.asmx的Web Service,作為Silverlight與Access數(shù)據(jù)庫(kù)互操作的橋梁。

創(chuàng)建完畢后,雙擊EmployeesInfoWebService.asmx打開(kāi)該文件。將里面的內(nèi)容修改如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Data.OleDb;//引入該命名空間為了操作Access數(shù)據(jù)庫(kù) using System.Data; namespace datagridnaccessdb { /// <summary> /// Summary description for EmployeesInfoWebService /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class EmployeesInfoWebService : System.Web.Services.WebService { [WebMethod]//獲取雇員信息 public List<EmployeeModel> GetEmployeesInfo() { List<EmployeeModel> returnedValue = new List<EmployeeModel>(); OleDbCommand Cmd = new OleDbCommand(); SQLExcute("SELECT * FROM Employee", Cmd); OleDbDataAdapter EmployeeAdapter = new OleDbDataAdapter(); EmployeeAdapter.SelectCommand = Cmd; DataSet EmployeeDataSet = new DataSet(); EmployeeAdapter.Fill(EmployeeDataSet); foreach (DataRow dr in EmployeeDataSet.Tables[0].Rows) { EmployeeModel tmp = new EmployeeModel(); tmp.EmployeeID = Convert.ToInt32(dr[0]); tmp.EmployeeName = Convert.ToString(dr[1]); tmp.EmployeeAge = Convert.ToInt32(dr[2]); returnedValue.Add(tmp); } return returnedValue; } [WebMethod] //添加雇員信息 public void Insert(List<EmployeeModel> employee) { employee.ForEach( x => { string CmdText = "INSERT INTO Employee(EmployeeName,EmployeeAge) VALUES('"+x.EmployeeName+"',"+x.EmployeeAge.ToString()+")"; SQLExcute(CmdText); }); } [WebMethod] //更新雇員信息 public void Update(List<EmployeeModel> employee) { employee.ForEach(x => { string CmdText = "UPDATE Employee SET EmployeeName='"+x.EmployeeName+"',EmployeeAge="+x.EmployeeAge.ToString(); CmdText += " WHERE EmployeeID="+x.EmployeeID.ToString(); SQLExcute(CmdText); }); } [WebMethod] //刪除雇員信息 public void Delete(List<EmployeeModel> employee) { employee.ForEach(x => { string CmdText = "DELETE FROM Employee WHERE EmployeeID="+x.EmployeeID.ToString(); SQLExcute(CmdText); }); } //執(zhí)行SQL命令文本,重載1 private void SQLExcute(string SQLCmd) { string ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath(@"App_Data\Employees.mdb;"); OleDbConnection Conn = new OleDbConnection(ConnectionString); Conn.Open(); OleDbCommand Cmd = new OleDbCommand(); Cmd.Connection = Conn; Cmd.CommandTimeout = 15; Cmd.CommandType = CommandType.Text; Cmd.CommandText = SQLCmd; Cmd.ExecuteNonQuery(); Conn.Close(); } //執(zhí)行SQL命令文本,重載2 private void SQLExcute(string SQLCmd,OleDbCommand Cmd) { string ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath(@"App_Data\Employees.mdb;"); OleDbConnection Conn = new OleDbConnection(ConnectionString); Conn.Open(); Cmd.Connection = Conn; Cmd.CommandTimeout = 15; Cmd.CommandType = CommandType.Text; Cmd.CommandText = SQLCmd; Cmd.ExecuteNonQuery(); } } }之后,在Silverlight客戶端應(yīng)用程序文件夾下,右擊References文件夾,選擇菜單選項(xiàng)Add Service Reference...。如下圖所示,引入剛才我們創(chuàng)建的Web Service(別忘了按Discover按鈕進(jìn)行查找)。

創(chuàng)建Silverlight客戶端應(yīng)用程序
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"> <Grid x:Name="LayoutRoot" Width="320" Height="240" Background="White"> <dataFormToolkit:DataForm x:Name="dfEmployee" Margin="8,8,8,42"/> <Button x:Name="btnGetData" Height="30" Margin="143,0,100,8" VerticalAlignment="Bottom" Content="Get Data" Width="77"/> <Button x:Name="btnSaveAll" Height="30" Margin="0,0,8,8" VerticalAlignment="Bottom" Content="Save All" HorizontalAlignment="Right" Width="77"/> <TextBlock x:Name="tbResult" Height="30" HorizontalAlignment="Left" Margin="8,0,0,8" VerticalAlignment="Bottom" Width="122" TextWrapping="Wrap" FontSize="16"/> </Grid> </UserControl> MainPage.xaml.cs文件 using System; using System.Collections.Generic; using System.Collections.ObjectModel; 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.Xml; using System.Xml.Linq; using System.Windows.Browser; using SilverlightClient.EmployeesInfoServiceReference; namespace SilverlightClient { public partial class MainPage : UserControl { int originalNum;//記錄初始時(shí)的Employee表中的數(shù)據(jù)總數(shù) ObservableCollection<EmployeeModel> deletedID = new ObservableCollection<EmployeeModel>();//標(biāo)記被刪除的對(duì)象 public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainPage_Loaded); this.btnGetData.Click += new RoutedEventHandler(btnGetData_Click); this.btnSaveAll.Click += new RoutedEventHandler(btnSaveAll_Click); this.dfEmployee.DeletingItem += new EventHandler<System.ComponentModel.CancelEventArgs>(dfEmployee_DeletingItem); } void dfEmployee_DeletingItem(object sender, System.ComponentModel.CancelEventArgs e) { deletedID.Add(dfEmployee.CurrentItem as EmployeeModel);//正在刪除時(shí),將被刪除對(duì)象進(jìn)行標(biāo)記,以便傳給服務(wù)端真正刪除。 } void btnSaveAll_Click(object sender, RoutedEventArgs e) { List<EmployeeModel> updateValues = dfEmployee.ItemsSource.Cast<EmployeeModel>().ToList(); ObservableCollection<EmployeeModel> returnValues = new ObservableCollection<EmployeeModel>(); if (updateValues.Count > originalNum) { //添加數(shù)據(jù) for (int i = originalNum; i <= updateValues.Count - 1; i++) { returnValues.Add(updateValues.ToArray()[i]); } EmployeesInfoWebServiceSoapClient webClient = new EmployeesInfoWebServiceSoapClient(); webClient.InsertCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(webClient_InsertCompleted); webClient.InsertAsync(returnValues); //必須考慮數(shù)據(jù)集中既有添加又有更新的情況 returnValues.Clear(); updateValues.ForEach(x => returnValues.Add(x)); webClient.UpdateCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(webClient_UpdateCompleted); webClient.UpdateAsync(returnValues); } else if (updateValues.Count < originalNum) { //刪除數(shù)據(jù) EmployeesInfoWebServiceSoapClient webClient = new EmployeesInfoWebServiceSoapClient(); webClient.DeleteCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(webClient_DeleteCompleted); webClient.DeleteAsync(deletedID); } else { //更新數(shù)據(jù) updateValues.ForEach(x => returnValues.Add(x)); EmployeesInfoWebServiceSoapClient webClient = new EmployeesInfoWebServiceSoapClient(); webClient.UpdateCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(webClient_UpdateCompleted); webClient.UpdateAsync(returnValues); } } void webClient_UpdateCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { tbResult.Text = "更新成功!"; } void webClient_DeleteCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { tbResult.Text = "刪除成功!"; } void webClient_InsertCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { tbResult.Text = "添加成功!"; } void btnGetData_Click(object sender, RoutedEventArgs e) { GetEmployees(); } void MainPage_Loaded(object sender, RoutedEventArgs e) { GetEmployees(); } void GetEmployees() { EmployeesInfoWebServiceSoapClient webClient = new EmployeesInfoWebServiceSoapClient(); webClient.GetEmployeesInfoCompleted += new EventHandler<GetEmployeesInfoCompletedEventArgs>(webClient_GetEmployeesInfoCompleted); webClient.GetEmployeesInfoAsync(); } void webClient_GetEmployeesInfoCompleted(object sender, GetEmployeesInfoCompletedEventArgs e) { originalNum = e.Result.Count;//記錄原始數(shù)據(jù)個(gè)數(shù) dfEmployee.ItemsSource = e.Result; } } }最終效果圖

“Silverlight與Access互操作的實(shí)現(xiàn)方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
當(dāng)前文章:Silverlight與Access互操作的實(shí)現(xiàn)方法
網(wǎng)頁(yè)URL:http://www.chinadenli.net/article40/iphjeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站維護(hù)、云服務(wù)器、做網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)