Powerful DataGrid component [2] _ ADO. NET Entity Framework of Data Interaction -- Silverlight Study Notes [10]

Source: Internet
Author: User
Tags sql server express

In the previous article, we discussed the basic data binding knowledge of the DataGrid. In today's tutorial, I will show you how to use the ADO. NET Entity Framework to perform basic interactions with databases.

Overview

To make things easier, I will first explain how to retrieve data from the database and return it to the DataGrid. This example shows the following functions:

1) Click the [get data] button to send a data retrieval message to the background.

2) the background database returns the relevant data to the DataGrid.

Here, if a large amount of test data is used, the obvious latency can be observed (this is the effect of asynchronous operations ).

Simple Preparation

To enable the ADO. NET Entity Framework to work properly, add the SP1 service package to your vs2008.

Appendix SP1:

Chinese version:

Http://www.microsoft.com/downloadS/details.aspx? Familyid = 27673c47-b3b5-4c67-bd99-84e525b5ce61 & displaylang = ZH-CN

English version:

Http://www.microsoft.com/downloadS/details.aspx? Familyid = 27673c47-b3b5-4c67-bd99-84e525b5ce61 & displaylang = en

Create a project

Click File> New> project... in the menu to open the new project dialog box. Select Web in project types, select ASP. NET web application in templates on the right, enter the project name testdatagrid, and click OK to complete the creation. (See Figure 1)

Figure 1: Create an ASP. NET web application

Right-click the root folder of solution 'testdatagrid 'in Solution Explorer, and choose add-> new project... from the menu options .... Select Silverlight in project types, select Silverlight application in templates on the right, enter the project name as silverlightclient, and click OK to complete the creation. (See figure 2 and Figure 3)

Figure 2: Create a Silverlight Project

Figure 3: Add a new project

In the displayed dialog box, click set and then click OK.

Figure 4: Create a Silverlight ApplicationProgram

Create a database

To speed up the experiment, we use SQL Server express 2008 as the background database.

Follow these steps to create a database and a data table:

1) Right-click the app_data folder in the testdatagrid folder and choose add> new item from the menu options .... In the displayed dialog box, enter the database name "employees. MDF" and click "add.

Figure 5: Create a database employees

Double-click employees. MDF to open the Server Explorer window. As shown in the following two figures, add a new data table and name it "employee.

Figure 6: Add a new data table

Figure 7: employee table settings

Enter some test data.

Figure 8: Enter some test data

Create an ADO. NET Entity database entity model

Right-click the testdatagrid folder and choose add-> new item... from the menu .... Name the database entity as employeemodel. edmx and click Add.

Figure 9: Create an object model

In the displayed dialog box, select generate from database and click Next. Follow the steps to finish the operation.

Figure 10: generate a model from a database

Figure 11: Select data link

Figure 12: select a database object

In this way, the data model is created.

Establish ADO. Net Data Service Data Communication Service

Right-click the testdatagrid folder and choose add> new item .... Then, name the ADO. NET data service as employeeinfoservice. SVC according to the operation.

Figure 13: Create ADO. NET data service

Modify employeeinfoservice. SVC. CSCodeAs follows:

 Using System; Using System. Collections. Generic; Using System. Data. Services; Using System. LINQ; Using System. servicemodel. Web; Using System. Web; Namespace Testdatagrid { Public class  Employeeinfoservice : Dataservice <Employeesentities > { Public static void Initializeservice ( Idataserviceconfiguration Config) {config. setentitysetaccessrule ( "*" , Entitysetrights . All); config. setserviceoperationaccessrule ( "*" , Serviceoperationrights . All );}}}

Press Ctrl + Shift + B to compile the entire project (very important, otherwise data service reference will go wrong !)

Right-click the created employeeinfoservice. SVC and select the menu option view in browser (for example). The page shown is displayed if the configuration is successful.

Figure 14: view web service configurations

Figure 15: page displayed when configuration is successful

Right-click the references in the silverlightclient project folder and select add service references .... Then, click the Discover button in the pop-up dialog box. The newly created employeeinfoservice is displayed in services. SVC, click "+" on the left to expand the symbol, and a service search will appear. After that, change the namespace to employeeservicereference. (SEE ).

Figure 16: Add a service reference

In this way, the process of establishing the ADO. NET data service data communication service is over.

Create the silverlightclient interface and component code

Mainpage. XAMLCode:

 <  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" X  :  Class  = "Silverlightclient. mainpage"  D  :  Designwidth  = "640"  D  :  Designheight  = "480"> <  Grid  X  :  Name  = "Layoutroot"  Background = "White"  Width  = "320"  Height  = "220"> <  Data  :  DataGrid  X  :  Name  = "Dgemployee"  Height  = "150"  Margin  = "8, 8, 0, 0"  Verticalalignment  = "TOP" Horizontalalignment  = "Left"  Width  = "304"/> <  Button  X  :  Name  = "Btngetdata"  Height  = "28"  Horizontalalignment  = "Left"  Margin  = "8,171"  Verticalalignment  = "TOP" Width  = "98"  Content  = "Get Data"/> </  Grid  > </  Usercontrol  > 

Mainpage. XAML. CSCode:

 Using System; Using System. Collections. Generic; 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. Data. Services. client; // Introduce the system. Data. Services. Client namespace  Using Silverlightclient. employeeservicereference; // Introduce the namespace of the Data Service  Namespace Silverlightclient {Public partial class  Mainpage : Usercontrol { Public Mainpage () {initializecomponent (); // Registration event trigger handling  This . Btngetdata. Click + = New  Routedeventhandler (Btngetdata_click );} Void Btngetdata_click ( Object Sender, Routedeventargs E ){ Employeesentities Proxy =New  Employeesentities ( New  Uri ( "Maid. SVC" , Urikind . Relative )); VaR Query = From C In Proxy. Employee Select C; Dataservicequery < Employee > Userquery = ( Dataservicequery <Employee >) Query; userquery. beginexecute ( New  Asynccallback (Onloadcomplete), query ); // Asynchronous call } Void Onloadcomplete ( Iasyncresult Result ){ Dataservicequery < Employee > Query = ( Dataservicequery < Employee >) Result. asyncstate; dgemployee. itemssource = query. endexecute (result). tolist (); // Pass the final result to the DataGrid }}}

 

Finally, press F5 to perform the compilation test.

Final

Author: kinglee
Article Source: kinglee's blog (http://www.cnblogs.com/Kinglee)
Copyright: The copyright of this article is shared by the author and the blog. The detailed link of this article must be noted during reprinting; otherwise, the author will be held legally liable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.