Microsoft Visual C# 2010 Step by Step (P13) doc

50 328 0
Microsoft Visual C# 2010 Step by Step (P13) doc

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

570 Part V Managing Data 7. If you are using Visual C# 2010 Express Edition, perform the following tasks: 7.1. Click New Connection. If the Choose Data Source dialog box appears, in the Data source list box, click Microsoft SQL Server Database File. In the Data provider drop-down list box, se- lect .NET Framework Data Provider for SQL Server if it is not already selected and then click Continue. Note If you have already created database connections previously, this dialog box might not appear and the Connection Properties dialog box will be displayed. In this case, click the Change button adjacent to the Data source text box. The Change Data Source dialog box appears, which is the same as the Choose Data Source dialog box except that the Continue button has the legend OK instead. 7.2. In the Connection Properties dialog box, in the Database file name text click Browse. 7.3. In the Select SQL Server Database File dialog box, move to the folder C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA, click the Northwind database file, and then click Open. 7.4. In the Log on to the server section of the dialog box, choose the Use Windows Authentication radio button and then click OK. 8. On the Choose Your Data Connection page of the Entity Data Model Wizard, select the Save entity connection settings in App.Config as check box, type NorthwindEntities (this is the default name), and then click Next. If you are using Visual C# 2010 Express Edition, a message box appears asking whether you want to add the database file to your project. Click No. 9. On the Choose Your Database Objects page, verify that the Pluralize or singularize gen- erated object names and Include foreign key columns in the model check boxes are both selected. In the Which database objects do you want to include in your model? list box, expand Tables and then click the Products (dbo) and Suppliers (dbo) tables. In the Model Namespace text box, type NorthwindModel (this is the default namespace). The following image shows the completed page. Chapter 26 Displaying and Editing Data by Using the Entity Framework and Data Binding 571 10. Click Finish. The Entity Data Model Wizard generates entity classes called Supplier and Product based on the Suppliers and Products tables, with property fields for each column in the tables, as shown in the following image. The data model also defines navigation prop- erties that link the two entities together and maintain the relationship between them. In this case, a single Supplier entity can be related to many Product entities. You can modify the properties of an entity class by selecting the class and changing the property values in the Properties window. You can also use the Mapping Details pane that appears at the bottom of the window to select and edit the fields that appear in 572 Part V Managing Data an entity class. This is how you change the mapping from the logical properties in an entity to the physical columns in a table. Important This exercise assumes that you are using the default entity classes generated for the Suppliers and Products tables in the database, so please do not change anything! 11. In Solution Explorer, expand the Northwind.edmx folder, and then double-click Northwind.designer.cs. Tip If Solution Explorer is not visible, on the View menu click Solution Explorer. The code generated by the Entity Data Model Wizard appears in the Code and Text Editor window. If you expand the Contexts region, you will see that it contains a class called NorthwindEntities that derives from the ObjectContext class. In the Entity Framework, the ObjectContext class performs a similar role to a DataContext class in LINQ to SQL, and you can use it to connect to the database. The NorthwindEntities class extends the ObjectContext class with logic to connect to the Northwind database, and to populate Supplier and Product entities (just like a custom DataContext class in LINQ to SQL). The information concerning the connection you specified before creating the two entity classes is saved in an application configuration file. Storing the connection string in a configuration file enables you to modify the connection string without rebuilding the application; you simply edit the application configuration file. You’ll find it useful if you envisage ever needing to relocate or rename the database, or switching from using a local development database to a production database that has the same set of tables. The code for the two entity classes is located in the Entities region of the file. These entity classes are a little more complicated than the classes that you created manually in Chapter 25, but the general principles are similar. The additional complexity is the result of the entity classes indirectly implementing the INotifyPropertyChanging and INotifyPropertyChanged interfaces, and the navigational properties used to link related entities together. The INotifyPropertyChanging and INotifyPropertyChanged interfaces define events that the entity classes raise when their property values change. The vari- ous user interface controls in the WPF library subscribe to these events to detect any changes to data and ensure that the information displayed on a WPF form is up to date. Note The entity classes inherit from the System.Data.Objects.DataClasses.EntityObject class, which in turn inherits from the System.Data.Objects.DataClasses.StructuralObject class. The StructuralObject class implements the the INotifyPropertyChanging and INotifyPropertyChanged interfaces. Chapter 26 Displaying and Editing Data by Using the Entity Framework and Data Binding 573 Using an Application Configuration File An application configuration file provides a useful mechanism enabling a user to mod- ify some of the resources used by an application without rebuilding the application it- self. The connection string used for connecting to a database is an example of just such a resource. When you use the Entity Data Model Wizard to generate entity classes, a new file is added to your project called App.config. This is the source for the application configu- ration file, and it appears in the Solution Explorer window. You can examine the con- tents of the App.config file by double-clicking it. You will see that it is an XML file, as shown here (the text has been reformatted to fit on the printed page): <?xml version="1.0" encoding="utf-8"?> <configuration> <connectionStrings> <add name="NorthwindEntities" connectionString="metadata=res://*/Northwind. csdl|res://*/Northwind.ssdl|res://*/Northwind.msl;provider=System.Data. SqlClient;provider connection string=&quot;Data Source=.\SQLExpress;Initial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /> </connectionStrings> </configuration> The connection string is held in the <connectionStrings> element of the file. This string contains a set of elements in the form property=value. The elements are separated by a semi-colon character. The key properties are the Data Source, Initial Catalog, and Integrated Security elements, which you should recognize from earlier exercises. When you build the application, the C# compiler copies the app.config file to the folder holding the compiled code and renames it as application.exe.config, where application is the name of your application. When your application connects to the database, it should read the connection string value from the configuration file rather than using values that are hard-coded in your C# code. You will see how to do this when using generated entity classes later in this chapter. You should deploy the application configuration file (the application.exe.config file) with the executable code for the application. If you need to connect to a different database, you can edit the configuration file by using a text editor to modify the < connectionString> attribute of the <connectionStrings> element. When the application runs, it will use the new value automatically. Be aware that you should take steps to protect the application configuration file and prevent a user from making inappropriate changes. 574 Part V Managing Data Now that you have created the entity model for the application, you can build the user inter- face that can display the information retrieved by using data binding. Create the user interface for the Suppliers application 1. In Solution Explorer, right-click the MainWindow.xaml file, click Rename, and rename the file as SupplierInfo.xaml. 2. Double-click the App.xaml file to display it in the Design View window. In the XAML pane, change the StartupUri element to “SupplierInfo.xaml”, as shown next in bold type: <Application x:Class="Suppliers.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="SupplierInfo.xaml"> </Application> 3. In Solution Explorer, double-click the SupplierInfo.xaml file to display it in the Design View window. In the XAML pane, as shown in bold type in the following code snip- pet, change the value of the x:Class element to “Suppliers.SupplierInfo”, set the Title to “Supplier Information”, set the Height to”362”, and set the Width to “614”: <Window x:Class="Suppliers.SupplierInfo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Supplier Information" Height="362" Width="614"> </Window> 4. Display the SupplierInfo.xaml.cs file in the Code and Text Editor window. Change the name of the MainWindow class to SupplierInfo, and change the name of the constructor, as shown next in bold type: public partial class SupplierInfo : Window { public SupplierInfo() { InitializeComponent(); } } 5. In Solution Explorer, double-click the SupplierInfo.xaml file to display it in the Design View window. From the Common WPF Controls section of the Toolbox, add a ComboBox control and a Button control to the form. (Place them anywhere on the form.) From the All WPF Controls section of the Toolbox, add a ListView control to the form. 6. Using the Properties window, set the properties of these controls to the values specified in the following table. Chapter 26 Displaying and Editing Data by Using the Entity Framework and Data Binding 575 Control Property Value comboBox1 Name suppliersList Height 23 Width Auto Margin 40,16,42,0 VerticalAlignment Top HorizontalAlignment Stretch listView1 Name productsList Height Auto Width Auto Margin 40,44,40,60 VerticalAlignment Stretch HorizontalAlignment Stretch button1 Name saveChanges Content Save Changes IsEnabled False (clear the check box) Height 23 Width 90 Margin 40,0,0,10 VerticalAlignment Bottom HorizontalAlignment Left The Supplier Information form should look like this in the Design View window: 576 Part V Managing Data 7. In the XAML pane, add the Window.Resources element shown next in bold type to the Window element, above the Grid element: <Window x:Class="Suppliers.SupplierInfo" > <Window.Resources> <DataTemplate x:Key="SuppliersTemplate"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=SupplierID}" /> <TextBlock Text=" : " /> <TextBlock Text="{Binding Path=CompanyName}" /> <TextBlock Text=" : " /> <TextBlock Text="{Binding Path=ContactName}" /> </StackPanel> </DataTemplate> </Window.Resources> <Grid> </Grid> </Window> You can use a DataTemplate to specify how to display data in a control. You will apply this template to the suppliersList combo box in the next step. This template contains five TextBlock controls organized horizontally by using a StackPanel. The first, third, and fifth TextBlock controls will display the data in the SupplierID, CompanyName, and ContactName properties, respectively, of the Supplier entity object to which you will bind later. The other TextBlock controls just display a “:” separator. 8. In the XAML pane, modify the definition of the suppliersList combo box and specify the IsSynchronizedWithCurrentItem, ItemsSource, and ItemTemplate properties, as shown next in bold type: <ComboBox Name="suppliersList" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" ItemTemplate="{StaticResource SuppliersTemplate}" /> Tip If you prefer, you can also set these properties by using the Properties window for the suppliersList combo box. You will display the data for each supplier in the suppliersList control. Recall from Chapter 25 that LINQ to SQL used Table<T> collection classes to hold the rows for a table. The Entity Framework follows a similar approach, but it holds the rows in an ObjectSet<T> collection class. Setting the IsSynchronizedWithCurrentItem property ensures that the SelectedItem property of the control is kept synchronized with the cur- rent item in the collection. If you don’t set this property to True, when the application starts up and establishes the binding with the collection, the combo box will not auto- matically display the first item in this collection. ItemsSource currently has an empty binding. In Chapter 24, you defined an instance of a class as a static resource and specified that resource as the binding source. If you Chapter 26 Displaying and Editing Data by Using the Entity Framework and Data Binding 577 do not specify a binding source, WPF binds to an object specified in the DataContext property of the control. (Do not confuse the DataContext property of a control with a DataContext object used by LINQ to SQL to communicate with a database; it is unfortu- nate that they happen to have the same name.) You will set the DataContext property of the control to an ObjectSet<Supplier> collection object in code. The ItemTemplate property specifies the template to use to display data retrieved from the binding source. In this case, the suppliersList control will display the SupplierID, CompanyName, and ContactName fields from the binding source. 9. Modify the definition of the productsList ListView, and specify the IsSynchronizedWithCurrentItem and ItemsSource properties: <ListView Name="productsList" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" /> The Supplier entity class contains an EntityCollection<Product> property that references the products the supplier can provide. (The EntityCollection<T> class is very similar to the EntitySet<T> class in LINQ to SQL.) You will set the DataContext property of the productsList control to the Products property of the currently selected Supplier object in code. In a later exercise, you will also provide functionality enabling the user to add and remove products. This code will modify the list of products acting as the binding source. Setting the IsSynchronizedWithCurrentItem property to True ensures that the newly created product is selected in the list when the user adds a new one or that an existing item is selected if the user deletes one. (If you set this property to False, when you delete a product, no item in the list will be selected afterwards, which can cause problems in your application if your code attempts to access the currently selected item.) 10. Add the ListView.View child element shown next in bold type, which contains a GridView and column definitions, to the productsList control. Be sure to replace the closing delimiter (/>) of the ListView element with an ordinary delimiter (>) and add a terminating </ListView> element. <ListView Name="productsList" > <ListView.View> <GridView> <GridView.Columns> <GridViewColumn Width="75" Header="Product ID" DisplayMemberBinding="{Binding Path=ProductID}" /> <GridViewColumn Width="225" Header="Name" DisplayMemberBinding="{Binding Path=ProductName}" /> <GridViewColumn Width="135" Header="Quantity Per Unit" DisplayMemberBinding="{Binding Path=QuantityPerUnit}" /> <GridViewColumn Width="75" Header="Unit Price" DisplayMemberBinding="{Binding Path=UnitPrice}" /> </GridView.Columns> </GridView> </ListView.View> </ListView> 578 Part V Managing Data You can make a ListView control display data in various formats by setting the View property. This Extensible Application Markup Language (XAML) code uses a GridView component. A GridView displays data in a tabular format; each row in the table has a fixed set of columns defined by the GridViewColumn properties. Each column has its own header that displays the name of the column. The DisplayMemberBinding property of each column specifies the data that the column should display from the binding source. The data for the UnitPrice column is a Decimal property in the Product entity class. WPF will convert this information to a string and apply a default numeric format. Ideally, the data in this column should be displayed as a currency value. You can reformat the data in a GridView column by creating a converter class. You first encountered converter classes in Chapter 24 when converting an enumeration to a string. This time, the con- verter class will convert a decimal? value to a string containing a representation of a currency value. 11. Switch to the Code and Text Editor window displaying the SupplierInfo.xaml.cs file. Add the PriceConverter class shown next to this file, after the SupplierInfo class: [ValueConversion(typeof(string), typeof(Decimal))] class PriceConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value != null) return String.Format("{0:C}", value); else return ""; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } The Convert method calls the String.Format method to create a string that uses the local currency format of your computer. The user will not actually modify the unit price in the list view, so there is no need to implement the ConvertBack method to convert a string back to a Decimal value. 12. Return to the Design View window displaying the SupplierInfo.xaml form. Add the following XML namespace declaration to the Window element, and define an instance of the PriceConverter class as a Window resource, as shown next in bold type: <Window x:Class="Suppliers.SupplierInfo" xmlns:app="clr-namespace:Suppliers" > <Window.Resources> <app:PriceConverter x:Key="priceConverter" /> Chapter 26 Displaying and Editing Data by Using the Entity Framework and Data Binding 579 </Window.Resources> </Window> Note The Design View window caches the definitions of controls and other user interface items, and does not always recognize new namespaces that have been added to a form immediately. If this statement causes an error in the Design View window, on the Build menu click Build Solution. This will refresh the WPF cache and should clear the errors. 13. Modify the definition of the Unit Price GridViewColumn, and apply the converter class to the binding, as shown next in bold type: <GridViewColumn Header ="Unit Price" DisplayMemberBinding= "{Binding Path=UnitPrice , Converter={StaticResource priceConverter}}" /> You have now laid out the form. Next, you need to write some code to retrieve the data displayed by the form, and you must set the DataContext properties of the suppliersList and productsList controls so that the bindings function correctly. Write code to retrieve supplier information and establish the data bindings 1. In the SupplierInfo.xaml file, change the definition of the Window element and add a Loaded event method called Window_Loaded. (This is the default name of this method, generated when you click <New Event Handler>.) The XAML code for the Window element should look like this: <Window x:Class="Suppliers.SupplierInfo" Title="Supplier Information" Loaded="Window_Loaded"> </Window> 2. In the Code and Text Editor window displaying the SupplierInfo.xaml.cs file, add the following using statements to the list at the top of the file: using System.ComponentModel; using System.Collections; 3. Add the three private fields shown next in bold type to the SupplierInfo class: public partial class SupplierInfo : Window { private NorthwindEntities northwindContext = null; private Supplier supplier = null; private IList productsInfo = null; } You will use the northwindContext variable to connect to the Northwind database and retrieve the data from the Suppliers table. The supplier variable holds the data for the [...]... database by using the Entity Framework Provide a handler for the OptimisticConcurrencyException In the e ­ xception handler, call the Refresh method of the ObjectContext object to retrieve the most recent data from the database for the original values in the cache, and then call SaveChanges again Microsoft Visual C# 2010 Step by Step Part VI Building Professional Solutions with Visual Studio 2010 In... the Dynamic Language Runtime to build C# applications and components that can interoperate with services built by using other languages that operate outside of the structure provided by the NET Framework, such as Python and Ruby In the bulk of the preceding chapters in this book, you learned how to use C# to write p ­ rograms that run in a single-threaded manner By “single-threaded,” I mean that at... want to continue to the next chapter Keep Visual Studio 2010 running, and turn to Chapter 27 n If you want to exit Visual Studio 2010 now On the File menu, click Exit If you see a Save dialog box, click Yes and save the project Chapter 26 Quick Reference To Do this Create entity classes by using the Entity Framework Add a new class to the project by using the ADO.NET Entity Data Model template Use the... return to Visual Studio 2010 596 Part V  Managing Data In this chapter, you learned how to use the Entity Framework to generate an entity model for a database You saw how to use the entity model from a WPF application by binding controls to collections of entities You also saw how to use LINQ to Entities to access data through an entity model n If you want to continue to the next chapter Keep Visual. .. data, close the form and return to Visual Studio 2010 Using LINQ to Entities to Query Data The previous exercise used data binding to fetch and display information through the Entity Framework rather than explicitly creating and running LINQ queries However, as mentioned previously, you can also retrieve information from a data model built by using the Entity Framework by using LINQ to Entities The syntax... Products property to an ObjectQuery variable Chapter 26  Displaying and Editing Data by Using the Entity Framework and Data Binding 583 LINQ to Entities supports most of the standard LINQ query operators, although there are a few exceptions described in the documentation supplied with Visual Studio 2010 You can join data from multiple tables, order the results, and perform operations such as... tasks with threads to improve responsiveness and throughput in graphical user i ­nterface (GUI) applications n Cancel long-running tasks, and handle exceptions raised by parallel operations You have now seen how to use Microsoft Visual C# to build applications that provide a graphical user interface and that can manage data held in a database These are common features of most modern systems However,... values of additional columns by using the properties exposed by the entity class You add the new entity to an ObjectSet collection by using the AddObject method To save the new entity to the database, call the SaveChanges method on the ObjectContext object The following code example creates a new Product entity and adds it to the list of products in the collection maintained by the NorthwindEntities context... of threads provided by the NET Framework, called the ThreadPool, and implements 604 Part VI  Building Professional Solutions with Visual Studio 2010 a queuing mechanism to distribute the workload across these threads When a program c ­ reates a Task object, the task is added to a global queue When a thread becomes available, the task is removed from the global queue and is executed by that thread The... and save the changes again The UpdateException handler Chapter 26  Displaying and Editing Data by Using the Entity Framework and Data Binding 595 reports the error to the user and then refreshes the cache from the database by specifying the RefreshMode.StoreWins parameter (This causes the changes made by the user to be discarded.) Note that the most meaningful data for this exception is held in the . are using Visual C# 2010 Express Edition, perform the following tasks: 7.1. Click New Connection. If the Choose Data Source dialog box appears, in the Data source list box, click Microsoft. type NorthwindEntities (this is the default name), and then click Next. If you are using Visual C# 2010 Express Edition, a message box appears asking whether you want to add the database file. You can set the values of additional columns by using the properties exposed by the entity class. You add the new entity to an ObjectSet collection by using the AddObject method. To save the new

Ngày đăng: 05/07/2014, 16:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan