日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
詳解Silverlight與Access互操作的具體實(shí)現(xiàn)

Silverlight與Access互操作是一個(gè)很基礎(chǔ)的問(wèn)題,主要涉及到數(shù)據(jù)庫(kù)的操作。Access屬于輕量級(jí)的數(shù)據(jù)庫(kù),應(yīng)用起來(lái)還是比較方便的。創(chuàng)新互聯(lián)推薦《
走向銀光 —— 一步一步學(xué)Silverlight》

成都創(chuàng)新互聯(lián)自2013年起,先為寶興等服務(wù)建站,寶興等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為寶興企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。

在開發(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)目文件夾下)

 
 
 
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace datagridnaccessdb
  5. {
  6.     public class EmployeeModel
  7.     {
  8.         public int EmployeeID { get; set; }
  9.         public string EmployeeName { get; set; }
  10.         public int EmployeeAge { get; set; }
  11.     }
  12. }

建立服務(wù)端Web Service

右擊服務(wù)端項(xiàng)目文件夾,選擇Add->New Item....,按下圖所示建立一個(gè)名為EmployeesInfoWebService.asmx的Web Service,作為Silverlight與Access數(shù)據(jù)庫(kù)互操作的橋梁。

創(chuàng)建完畢后,雙擊EmployeesInfoWebService.asmx打開該文件。將里面的內(nèi)容修改如下:

 
 
 
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Services;
  6. using System.Data.OleDb;//引入該命名空間為了操作Access數(shù)據(jù)庫(kù)
  7. using System.Data;
  8. namespace datagridnaccessdb
  9. {
  10.     /// 
  11.     /// Summary description for EmployeesInfoWebService
  12.     /// 
  13.     [WebService(Namespace = "http://tempuri.org/")]
  14.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  15.     [System.ComponentModel.ToolboxItem(false)]
  16.     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
  17.     // [System.Web.Script.Services.ScriptService]
  18.     public class EmployeesInfoWebService : System.Web.Services.WebService
  19.     {
  20.         [WebMethod]//獲取雇員信息
  21.         public List GetEmployeesInfo()
  22.         {
  23.             List returnedValue = new List();
  24.             OleDbCommand Cmd = new OleDbCommand();
  25.             SQLExcute("SELECT * FROM Employee", Cmd);
  26.             OleDbDataAdapter EmployeeAdapter = new OleDbDataAdapter();
  27.             EmployeeAdapter.SelectCommand = Cmd;
  28.             DataSet EmployeeDataSet = new DataSet();
  29.             EmployeeAdapter.Fill(EmployeeDataSet);
  30.             foreach (DataRow dr in EmployeeDataSet.Tables[0].Rows)
  31.             {
  32.                 EmployeeModel tmp = new EmployeeModel();
  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.             return returnedValue;
  39.         }
  40.        [WebMethod] //添加雇員信息
  41.         public void Insert(List employee)
  42.         {
  43.             employee.ForEach( x => 
  44.            {
  45.                 string CmdText = "INSERT INTO Employee(EmployeeName,EmployeeAge) VALUES('"+x.EmployeeName+"',"+x.EmployeeAge.ToString()+")";
  46.                 SQLExcute(CmdText);
  47.             });
  48.         }
  49.         [WebMethod] //更新雇員信息
  50.        public void Update(List employee)
  51.         {
  52.            employee.ForEach(x =>
  53.             {
  54.                 string CmdText = "UPDATE Employee SET EmployeeName='"+x.EmployeeName+"',EmployeeAge="+x.EmployeeAge.ToString();
  55.                 CmdText += " WHERE EmployeeID="+x.EmployeeID.ToString();
  56.                 SQLExcute(CmdText);
  57.             });
  58.         }
  59.         [WebMethod] //刪除雇員信息
  60.        public void Delete(List employee)
  61.         {
  62.             employee.ForEach(x =>
  63.             {
  64.                 string CmdText = "DELETE FROM Employee WHERE EmployeeID="+x.EmployeeID.ToString();
  65.                 SQLExcute(CmdText);
  66.             });
  67.         }
  68.        //執(zhí)行SQL命令文本,重載1
  69.         private void SQLExcute(string SQLCmd)
  70.         {
  71.             string ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath(@"App_Data\Employees.mdb;");
  72.             OleDbConnection Conn = new OleDbConnection(ConnectionString);
  73.             Conn.Open();
  74.             OleDbCommand Cmd = new OleDbCommand();
  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.         //執(zhí)行SQL命令文本,重載2
  83.         private void SQLExcute(string SQLCmd,OleDbCommand Cmd)
  84.         {
  85.             string ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath(@"App_Data\Employees.mdb;");
  86.             OleDbConnection Conn = new OleDbConnection(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客戶端應(yīng)用程序文件夾下,右擊References文件夾,選擇菜單選項(xiàng)Add Service Reference...。如下圖所示,引入剛才我們創(chuàng)建的Web Service(別忘了按Discover按鈕進(jìn)行查找)。

創(chuàng)建Silverlight客戶端應(yīng)用程序

 
 
 
 
  1. MainPage.xaml文件
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. 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"
  5.     d:DesignWidth="320" d:DesignHeight="240">
  6.  x:Name="LayoutRoot" Width="320" Height="240" Background="White">
  7.   x:Name="dfEmployee" Margin="8,8,8,42"/>
  8.  x:Name="btnGetData" Height="30" Margin="143,0,100,8" VerticalAlignment="Bottom" Content="Get Data" Width="77"/>
  9.  x:Name="btnSaveAll" Height="30" Margin="0,0,8,8" VerticalAlignment="Bottom" Content="Save All" HorizontalAlignment="Right" Width="77"/>
  10.  x:Name="tbResult" Height="30" HorizontalAlignment="Left" Margin="8,0,0,8" VerticalAlignment="Bottom" Width="122" TextWrapping="Wrap" FontSize="16"/>
  11. MainPage.xaml.cs文件
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Collections.ObjectModel;
  15. using System.Linq;
  16. using System.Net;
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. using System.Windows.Documents;
  20. using System.Windows.Input;
  21. using System.Windows.Media;
  22. using System.Windows.Media.Animation;
  23. using System.Windows.Shapes;
  24. using System.Xml;
  25. using System.Xml.Linq;
  26. using System.Windows.Browser;
  27. using SilverlightClient.EmployeesInfoServiceReference;
  28. namespace SilverlightClient
  29. {
  30.     public partial class MainPage : UserControl
  31.     {
  32.         int originalNum;//記錄初始時(shí)的Employee表中的數(shù)據(jù)總數(shù)
  33.         ObservableCollection deletedID = new ObservableCollection();//標(biāo)記被刪除的對(duì)象
  34.        public MainPage()
  35.         {
  36.             InitializeComponent();
  37.             this.Loaded += new RoutedEventHandler(MainPage_Loaded);
  38.             this.btnGetData.Click += new RoutedEventHandler(btnGetData_Click);
  39.             this.btnSaveAll.Click += new RoutedEventHandler(btnSaveAll_Click); 
  40.             this.dfEmployee.DeletingItem += new EventHandler(dfEmployee_DeletingItem);
  41.         }
  42.         void dfEmployee_DeletingItem(object sender, System.ComponentModel.CancelEventArgs e)
  43.         {
  44.             deletedID.Add(dfEmployee.CurrentItem as EmployeeModel);//正在刪除時(shí),將被刪除對(duì)象進(jìn)行標(biāo)記,以便傳給服務(wù)端真正刪除。
  45.         }
  46.         void btnSaveAll_Click(object sender, RoutedEventArgs e)
  47.         {
  48.             List updateValues = dfEmployee.ItemsSource.Cast().ToList();
  49.             ObservableCollection returnValues = new ObservableCollection();
  50.             if (updateValues.Count > originalNum)
  51.             {
  52.                 //添加數(shù)據(jù)
  53.                 for (int i = originalNum; i <= updateValues.Count - 1; i++)
  54.                 {
  55.                     returnValues.Add(updateValues.ToArray()[i]);
  56.                 }
  57.                 EmployeesInfoWebServiceSoapClient webClient = new EmployeesInfoWebServiceSoapClient();
  58.                 webClient.InsertCompleted += new EventHandler(webClient_InsertCompleted);
  59.                 webClient.InsertAsync(returnValues);
  60.                 //必須考慮數(shù)據(jù)集中既有添加又有更新的情況
  61.                 returnValues.Clear();
  62.                 updateValues.ForEach(x => returnValues.Add(x));
  63.                 webClient.UpdateCompleted += new EventHandler(webClient_UpdateCompleted);
  64.                 webClient.UpdateAsync(returnValues);
  65.             }
  66.             else if (updateValues.Count < originalNum)
  67.             {
  68.                 //刪除數(shù)據(jù)
  69.                 EmployeesInfoWebServiceSoapClient webClient = new EmployeesInfoWebServiceSoapClient();
  70.                 webClient.DeleteCompleted += new EventHandler(webClient_DeleteCompleted);
  71.                 webClient.DeleteAsync(deletedID);
  72.             }
  73.             else
  74.             {
  75.                //更新數(shù)據(jù)
  76.                 updateValues.ForEach(x => returnValues.Add(x));
  77.                 EmployeesInfoWebServiceSoapClient webClient = new EmployeesInfoWebServiceSoapClient();
  78.                 webClient.UpdateCompleted += new EventHandler(webClient_UpdateCompleted);
  79.                 webClient.UpdateAsync(returnValues);
  80.            }
  81.         }
  82.         void webClient_UpdateCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
  83.         {
  84.             tbResult.Text = "更新成功!";
  85.         }
  86.         void webClient_DeleteCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
  87.         {
  88.             tbResult.Text = "刪除成功!";
  89.         }
  90.         void webClient_InsertCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
  91.         {
  92.             tbResult.Text = "添加成功!";
  93.         }
  94.         void btnGetData_Click(object sender, RoutedEventArgs e)
  95.         {
  96.             GetEmployees();
  97.         }
  98.         void MainPage_Loaded(object sender, RoutedEventArgs e)
  99.         {
  100.             GetEmployees();
  101.         }
  102.         void GetEmployees()
  103.         {
  104.             EmployeesInfoWebServiceSoapClient webClient = new EmployeesInfoWebServiceSoapClient();
  105.             webClient.GetEmployeesInfoCompleted +=
  106.             new EventHandler(webClient_GetEmployeesInfoCompleted);
  107.             webClient.GetEmployeesInfoAsync();
  108.         }
  109.         void webClient_GetEmployeesInfoCompleted(object sender, GetEmployeesInfoCompletedEventArgs e)
  110.         {
  111.             originalNum = e.Result.Count;//記錄原始數(shù)據(jù)個(gè)數(shù)
  112.             dfEmployee.ItemsSource = e.Result;
  113.         }
  114.     }
  115. }

最終效果圖

鏈接:http://www.cnblogs.com/Kinglee/archive/2009/09/05/1561021.html


網(wǎng)站名稱:詳解Silverlight與Access互操作的具體實(shí)現(xiàn)
轉(zhuǎn)載注明:http://m.5511xx.com/article/ccsdhch.html