Tài liệu Practical Database Programming With Visual C#.NET- P3 pptx

50 635 0
Tài liệu Practical Database Programming With Visual C#.NET- P3 pptx

Đ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

3.4 Components of ADO.NET 2.0 123 In addition to a schema, a DataTable must also have rows to contain and order data. The DataRow class represents the actual data contained in a table. You use the DataRow and its properties and methods to retrieve, evaluate, and manipulate the data in a table. As you access and change the data within a row, the DataRow object maintains both its current and original state. 3.4.7.1 Data T able Constructor The DataTable has four overloaded constructors and Table 3.18 lists three of the most often used constructors. You can create a DataTable object by using the appropriate DataTable constructor. You can add it to the DataSet by using the Add method to add it to the DataTable object ’ s Tables collection. You can also create DataTable objects within a DataSet by using the Fill() or FillSchema() methods of the DataAdapter object, or from a predefi ned or inferred XML schema using the ReadXml(), ReadXmlSchema(), or InferXmlSchema() methods of the DataSet. Note that after you have added a DataTable as a member of the Tables collec- tion of one DataSet, you cannot add it to the collection of tables of any other DataSet. When you fi rst create a DataTable, it does not have a schema (that is, a structure). To defi ne the schema of the table, you must create and add DataColumn objects to the Columns collection of the table. You can also defi ne a primary key column for the table and create and add Constraint objects to the Constraints collection of the table. After you have defi ned the schema for a DataTable, you can add rows of data to the table by adding DataRow objects to the Rows collection of the table. You are not required to supply a value for the TableName property when you create a DataTable; you can specify the property at another time, or you can leave it empty. However, when you add a table without a TableName value to a DataSet, the table will be given an incremental default name of Table N , starting with “ Table ” for Table0. Figure 3.17 shows an example of creating a new DataTable and a DataSet and then adding the DataTable into the DataSet object. Table 3.18 Three Popular Constructors of the Data T able Class Constructors Descriptions DataTable() Initializes a new instance of the DataTable class with no arguments. DataTable(String) Initializes a new instance of the DataTable class with the specifi ed table name. DataTable(String, String) Initializes a new instance of the DataTable class using the specifi ed table name and namespace. DataSet FacultyDataSet; DataTable FacultyTable; FacultyDataSet = new DataSet(); FacultyTable = new DataTable(“Faculty”); FacultyDataSet.Tables.Add(FacultyTable); Figure 3.17 Example of adding a DataTable into a DataSet. c03.indd 123c03.indd 123 2/11/2010 11:50:59 AM2/11/2010 11:50:59 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 124 Chapter 3 Introduction to ADO.NET First, you need to create an instance for both the DataSet and the DataTable classes, respectively. Then you can add this new DataTable instance into the new DataSet object by using the Add() method. 3.4.7.2 DataTable Properties The DataTable class has more than 20 properties. Table 3.19 lists some of the most often used properties. Among these properties, the Columns and Rows properties are very important to us, and both properties are collections of DataColumn and DataRow in the current DataTable object. The Columns property contains a collection of DataColumn objects in the current DataTable and each column in the table can be considered as a DataColumn object and can be added into this Columns collection. A similar situation happened to the Rows property. The Rows property contains a collection of DataRow objects that are composed of all rows in the current DataTable object. You can get the total number of columns and rows from the current DataTable by calling these two properties. 3.4.7.3 DataTable Methods The DataTable class has about 50 different methods with 33 public methods, and Table 3.20 lists some of the most often used methods. Among these methods, three of them are important to us: NewRow(), ImportRow(), and LoadDataRow(). Calling the NewRow() adds a row to the data table using the existing table schema, but with default values for the row, and sets the DataRowState to Added. Calling the ImportRow() pre- serves the existing DataRowState along with other values in the row. Calling the LoadDataRow() is to fi nd and update a data row from the current data table. This method Table 3.19 Popular Properties of the Data T able Class Properties Descriptions Columns The data type of the Columns property is DataColumnCollection, which means that it contains a collection of DataColumn objects. Each column in the DataTable can be considered as a DataColumn object. By calling this property, a collection of DataColumn objects that exists in the DataTable can be retrieved. DataSet Gets the DataSet to which this table belongs. IsInitialized Gets a value that indicates whether the DataTable is initialized. Namespace Gets or sets the namespace for the XML representation of the data stored in the DataTable. PrimaryKey Gets or sets an array of columns that function as primary keys for the data table. Rows The data type of the Rows property is DataRowCollection, which means that it contains a collection of DataRow objects. Each row in the DataTable can be considered as a DataRow object. By calling this property, a collection of DataRow objects that exists in the DataTable can be retrieved. TableName Gets or sets the name of the DataTable. c03.indd 124c03.indd 124 2/11/2010 11:50:59 AM2/11/2010 11:50:59 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 3.4 Components of ADO.NET 2.0 125 Table 3.20 Popular Methods of the Data T able Class Methods Descriptions Clear Clears the DataTable of all data. Copy Copies both the structure and data for this DataTable. Dispose Releases the resources used by the MarshalByValueComponent. GetChanges Gets a copy of the DataTable containing all changes made to it since it was last loaded or since AcceptChanges was called. GetType Gets the Type of the current instance. ImportRow Copies a DataRow into a DataTable, preserving any property settings, as well as original and current values. Load Fills a DataTable with values from a data source using the supplied IDataReader. If the DataTable already contains rows, the incoming data from the data source is merged with the existing rows. LoadDataRow Finds and updates a specifi c row. If no matching row is found, a new row is created using the given values. Merge Merge the specifi ed DataTable with the current DataTable. NewRow Creates a new DataRow with the same schema as the table. ReadXml Reads XML schema and data into the DataTable. RejectChanges Rolls back all changes that have been made to the table since it was loaded or the last time AcceptChanges was called. Reset Resets the DataTable to its original state. Select Gets an array of DataRow objects. ToString Gets the TableName and DisplayExpression, if there is one as a concatenated string. WriteXml Writes the current contents of the DataTable as XML. has two arguments, the Value (Object) and the Accept Condition (Boolean). The Value is used to update the data row if that row were found, and the Condition is used to indi- cate whether the table allows this update to be made or not. If no matching row is found, a new row is created with the given Value. 3.4.7.4 Data T able Events The DataTable class contains 11 public events and Table 3.21 lists these events. The most often used events are ColumnChanged, Initialized, RowChanged, and RowDeleted. By using these events, one can track and monitor the real situations that occur in the DataTable. Before we can fi nish this section, we need to show users how to create a data table and how to add data columns and rows into this new table. Figure 3.18 shows a complete example of creating a new data table object and adding columns and rows into this table. The data table is named FacultyTable. Refer to Figure 3.18 , starting from step A , a new instance of the data table FacultyTable is created and initialized to a blank table. In order to add data into this new table, you need to use the Columns and Rows collections, and these two collections c03.indd 125c03.indd 125 2/11/2010 11:50:59 AM2/11/2010 11:50:59 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 126 Chapter 3 Introduction to ADO.NET Table 3.21 Public Events of the Data T able Class Events Descriptions ColumnChanged Occurs after a value has been changed for the specifi ed DataColumn in a DataRow. ColumnChanging Occurs when a value is being changed for the specifi ed DataColumn in a DataRow. Disposed Adds an event handler to listen to the Disposed event on the component. Initialized Occurs after the DataTable is initialized. RowChanged Occurs after a DataRow has been changed successfully. RowChanging Occurs when a DataRow is changing. RowDeleted Occurs after a row in the table has been deleted. RowDeleting Occurs before a row in the table is about to be deleted. TableCleared Occurs after a DataTable is cleared. TableClearing Occurs when a DataTable is being cleared. TableNewRow Occurs when a new DataRow is inserted. //Create a new DataTable DataTable FacultyTable = new DataTable(" FacultyTable System.int32 FacultyId System.string FacultyOffice FacultyId FacultyOffice TC- "); //Declare DataColumn and DataRow variables DataColumn column; DataRow row; //Create new DataColumn, set DataType, ColumnName and add to DataTable column = new DataColumn(); column.DataType = System.Type.GetType(" "); column.ColumnName = " "; FacultyTable.Columns.Add(column); //Create another column. column = new DataColumn(); column.DataType = Type.GetType(" "); column.ColumnName = " "; FacultyTable.Columns.Add(column); //Create new DataRow objects and add to DataTable. int Index = 0; for (Index = 1; Index <= 10; Index++) { row = FacultyTable.NewRow(); row(" ") = Index; row(" ") = " " + Index; FacultyTable.Rows.Add(row); } A B C D E F Figure 3.18 Example of creating a new table and adding data into the table. contain the DataColumn and DataRow objects. So next you need to create DataColumn and DataRow objects, respectively. Step B fi nished these object declarations. In step C , a new instance of the DataColumn, column, is created by using the new keyword. Two DataColumn properties, DataType and ColumnName, are used to initial- ize the fi rst DataColumn object with the data type as integer (System.int32) and with the column name as “ FacultyId ” , respectively. Finally the completed object of the DataColumn c03.indd 126c03.indd 126 2/11/2010 11:50:59 AM2/11/2010 11:50:59 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 3.4 Components of ADO.NET 2.0 127 is added into the FacultyTable using the Add() method of the Columns collection class. In step D , the second data column, with the column data type as string (System.string) and the column name as the “ FacultyOffi ce ” , is added into the FacultyTable in a similar way as we did for the fi rst data column FacultyId. In step E , a for loop is utilized to simplify the procedure of adding new data rows into this FacultyTable. First a loop counter Index is created, and a new instance of the DataRow is created with the method of the DataTable — NewRow(). In total we create and add 10 rows into this FacultyTable object. For the fi rst column “ FacultyId ” , the loop counter Index is assigned to this column for each row. But for the second column “ FacultyOffi ce ” , the building name combined with the loop counter Index is assigned to this column for each row. Finally in step F , the DataRow object, row, is added into this FacultyTable using the Add() method that belongs to the Rows collection class. When this piece of codes runs, a complete FacultyTable can be obtained and it should match the one shown in Table 3.22 . 3.4.8 ADO . NET 3.5 Entity Framework Most traditional databases use the relational model of data, such as Microsoft Access, SQL Server, and Oracle. But today almost all programming languages are object - oriented lan- guages, and the object - oriented model of data structures are widely implemented in modern programs developed with those languages. Therefore, a potential contradiction exists between the relational model of data in databases and the object - oriented model of programming applied today. Although some new components were added into the ADO. NET 2.0 to try to solve this contradiction, still it does not give a full solution for this issue. A revolutionary solution of this problem came with the release of ADO.NET 3.5 based on the .NET Framework 3.5 and the addition of Language Integrated Query (LINQ) to Visual Studio.NET 2008. The main contributions of the ADO.NET 3.5 include some new components: ADO.NET 3.5 Entity Framework (ADO.NET 3.5 EF) and ADO. NET 3.5 Entity Data Model Tools are added into ADO.NET 3.5. With these new com- ponents, the contradiction exists between the relational model of data used in databases and the object - oriented programming projects that can be fully resolved. Table 3.22 Completed Faculty T able FacultyId FacultyOffi ce 1 TC - 1 2 TC - 2 3 TC - 3 4 TC - 4 5 TC - 5 6 TC - 6 7 TC - 7 8 TC - 8 9 TC - 9 10 TC - 10 c03.indd 127c03.indd 127 2/11/2010 5:15:14 PM2/11/2010 5:15:14 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 128 Chapter 3 Introduction to ADO.NET A primary goal of the ADO.NET 3.5 EF is to raise the level of abstraction available for data programming, thus simplifying the development of data - aware applications and enabling developers to write less code. The Entity Framework is the evolution of ADO. NET that allows developers to program in terms of the standard ADO.NET 3.5 abstrac- tion or in terms of persistent objects (Object Relational Mapper ORM) and is built upon the standard ADO.NET 3.5 Provider model, which allows access to third - party databases. The Entity Framework introduces a new set of services around the Entity Data Model (EDM) (a medium for defi ning domain models for an application). ADO.NET 3.5 provides an abstract database structure that converts the traditional logic database structure to an abstract or object structure with three layers: • Conceptual layer • Mapping layer • Logical layer ADO.NET 3.5 EF defi nes these three layers using a group of XML fi les, and these XML fi les provide a level of abstraction to enable users to program against the object - oriented Conceptual model instead of the traditional relational data model. The Conceptual layer provides a way to allow developers to build object - oriented codes to access databases, and each component in databases can be considered as an object or entity in this layer. The Conceptual Schema Defi nition Language (CSDL) is used in those XML fi les to defi ne entities and relationships that will be recognized and used by the Mapping layer to setup mapping between entities and relational data tables. The Mapping layer uses Mapping Schema Language (MSL) to establish mappings between entities in the Conceptual layer and the relational data structure in the Logical layer. The relational database schema is defi ned in an XML fi le using Store Schema Defi nition Language (SSDL) in the Logical layer. The Mapping layer works as a bridge or a converter to connect the Conceptual layer to the Logical layer and interpret between the object - oriented data model in the Conceptual layer and the relational data model in the Logical layer. This mapping, shown in Figure 3.19 , allows users to code against the Conceptual layer and map those codes into the Logical layer. Conceptual Layer Mapping Layer Logical Layer Relational Data Model Object-Oriented Model Figure 3.19 Mapping relationship between three layers. c03.indd 128c03.indd 128 2/11/2010 11:50:59 AM2/11/2010 11:50:59 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 3.4 Components of ADO.NET 2.0 129 A useful data component is provided by the Conceptual layer to enable users to develop object - oriented codes and it is called EntityClient. The EntityClient is a Data Provider with the associated components such as Connection (EntityConnection), Command (EntityCommand), and DataReader (EntityDataReader). The EntityClient is similar to other Data Providers we discussed in the previous sections in this chapter, but it includes new components and functionalities. The core of ADO.NET 3.5 EF is its Entity Data Model (EDM), and the user can access and use this model using the ADO.NET 3.5 Entity Data Model Tools that includes the EDM item template, the EDM wizard, the EDM Designer, entity mapping details, and the entity model browser. In the following sections, we will discuss the Entity Data Model and how to use these EDM Tools to create, build, and develop the Entity Data Model and implement it in actual data - driven applications. First let ’ s take a closer look at the ADO.NET 3.5 Entity Data Model. 3.4.8.1 ADO . NET 3.5 Entity Data Model The ADO.NET 3.5 Entity Data Model (EDM) is a data model for defi ning application data as sets of entities and relationships to which common language runtime (CLR) types and storage structures can be mapped. This enables developers to create data access applications by programming against a conceptual application model instead of program- ming directly against a relational storage schema. The following tools are designed to help you work with the EDM: • The ADO.NET 3.5 Entity Data Model item template is available for Visual C# project type, and ASP.NET Web Site and Web Application projects, and launches the EDM Wizard. • The EDM Wizard generates an EDM, which is encapsulated in an .edmx fi le. The wizard can generate the EDM from an existing database. The wizard also adds a connection string to the App.Confi g or Web.Confi g fi le and confi gures a single - fi le generator to run on the conceptual model contained in the .edmx fi le. This single - fi le generator will generate C# or VB code from the conceptual model defi ned in the .edmx fi le. • The ADO.NET EDM Designer provides visual tools to view and edit the EDM graphically. You can open an .edmx fi le in the designer and create entities and map entities to database tables and columns. • EdmGen.exe is a command - line tool that can be used to also generate models, validate existing models, and perform other functions on your EDM metadata fi les. We will provide a detailed discussion for each of these tools in the following sections. 3.4.8.1.1 Entity Data Model Item Template The ADO.NET 3.5 EDM item tem- plate is the starting point to the EDM tools. The ADO.NET 3.5 EDM item template is available for Visual C# and Visual Basic project types. It can be added to Console Application, Windows Application, Class Library, ASP.NET Web Service Application, ASP.NET Web Application, or ASP.NET Web Site projects. You can add multiple ADO.NET 3.5 EDM items to the same project, with each item containing fi les that were generated from a different database and/or tables within the same database. c03.indd 129c03.indd 129 2/11/2010 11:50:59 AM2/11/2010 11:50:59 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 130 Chapter 3 Introduction to ADO.NET When you add the ADO.NET 3.5 EDM item template to your project, Visual Studio: • Adds references to the System.Data, System.Data.Entity, System.Core, System. Security, and System.Runtime.Serialization assemblies if the project does not already have them. • Starts the EDM Wizard. The wizard is used to generate an EDM from an existing database. The wizard creates an .edmx fi le, which contains the model information. You can use the .edmx fi le in the ADO.NET EDM Designer to view or modify the model. • Creates a source code fi le that contains the classes generated from the conceptual model. The source code fi le is autogenerated and is updated when the .edmx fi le changes, and is compiled as part of the project. Next let ’ s have a look at the EDM Wizard. 3.4.8.1.2 Entity Data Model Wizard The EDM Wizard starts after you add an ADO. NET 3.5 Entity Data Model to your project. The wizard is used to generate an EDM. The wizard creates an .edmx fi le that contains the model information. The .edmx fi le is used by the ADO.NET 3.5 EDM Designer, which enables you to view and edit the map- pings graphically. You can select to create an empty model or to generate the model from an existing database. Generating the model from an existing database is the recommended practice for this release of the EDM tools. The Wizard also creates a source code fi le that contains the classes generated from the CSDL information encapsulated in the .edmx fi le. The source code fi le is autogene- rated and is updated when the .edmx fi le changes. Depending on your selections, the Wizard will help you with the following steps: • Choose the Model Contents: It is recommended that you select to generate the model from an existing database. The Wizard steps you through selecting the data source, database, and database objects to include in the EDM. • Choose the Database Connection: You can choose an existing connection from the list of connections or click New Database Connection to open the Connection Properties dialog box and create a new connection to the database . • Choose your Database Objects: You can select the tables, views, and stored procedures to include in the EDM. Now let ’ s have a look at the real part — ADO.NET 3.5 Entity Data Model Designer. 3.4.8.1.3 Entity Data Model Designer The ADO.NET 3.5 EDM Designer provides visual tools for creating and editing an EDM. The ADO.NET EDM Designer includes the following components: • A visual design surface for creating and editing the conceptual model. You can create, modify, or delete entities and associations. • An Entity Mapping Details window to view and edit mappings. You can map entity types or associations to database tables and columns. • An Entity Model Browser to give you a tree view of the EDM. • Toolbox controls to create entities, associations, and inheritance relationships. c03.indd 130c03.indd 130 2/11/2010 11:50:59 AM2/11/2010 11:50:59 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 3.4 Components of ADO.NET 2.0 131 The ADO.NET 3.5 EDM Designer is integrated with the Visual Studio.NET 2008 components. You can view and edit information using the Properties window and errors are reported in the Error List. Figure 3.20 shows an example of the ADO.NET 3.5 EDM Designer. Two important functionalities of using the EDM Designer are: Opening the ADO . NET Entity Data Model Designer The ADO.NET 3.5 EDM Designer is designed to work with an .edmx fi le. The .edmx fi le is an encapsulation of three EDM metadata artifact fi les, the CSDL, the SSDL, and the MSL fi les. When you run the EDM Wizard, an .edmx fi le is created and added to your solution. You open the ADO.NET EDM Designer by double - clicking on the .edmx fi le in the Solution Explorer. Validating the EDM As you make changes to the EDM, the ADO.NET EDM Designer validates the modifi cations and reports errors in the Error List. You can also validate the EDM at any time by right - clicking on the design surface and selecting Validate Model . 3.4.8.1.4 Entity Model Browser The Entity Model Browser is a Visual Studio tool window that is integrated with the ADO.NET 3.5 EDM Designer. It provides a tree view of the EDM. The Entity Model Browser groups the information into two nodes. The fi rst node shows you the conceptual model. By expanding the underlying nodes, you can view all entity types and associations in the model. Figure 3.20 Example of the ADO.NET 3.5 Entity Data Model Designer. c03.indd 131c03.indd 131 2/11/2010 11:50:59 AM2/11/2010 11:50:59 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 132 Chapter 3 Introduction to ADO.NET The second node shows you the target database model. By expanding the underlying nodes you can see what parts of the database tables, views, and stored procedures have been imported into the model. The EDM Browser enables you to do the following: • Clicking on an item in the Entity Model Browser makes it active in the Properties window and the Entity Mapping Details View window. You can use these windows to modify the properties or entity mappings. • Create a function to import a stored procedure. • Update the SSDL information from the database. The Entity Model Browser opens when the ADO.NET 3.5 EDM Designer is opened. If the Entity Model Browser is not visible, right - click on the main design surface and select Show Entity Model Browser . 3.4.8.2 Using ADO.NET 3.5 Entity Data Model Wizard In this section, we will use a project example to illustrate how to use the EDM Wizard to develop a data - driven application to connect to our database, to create entity classes, to set up associations between entities, and to set up mapping relationships between enti- ties and data tables in our database. Creating applications using the EDM can be signifi - cantly simplifi ed by using the ADO.NET EDM item template and the EDM Wizard. This section steps you through the following tasks: • Create a new Visual C# Windows - based application. • Use the EDM Wizard to select a data source and generate an EDM from our CSE_DEPT database. • Use the entities in this application. Let ’ s begin with creating a new Visual C# Windows - based project named EDModel. 3.4.8.2.1 Create New Visual C# Windows -Based Project Open Visual Studio. NET 2008 and select File|New|Project items to create a new project. Select the Visual C# as the project type and Windows Forms Applications as the Template for this new project. Enter EDModel into the Name box and select any folder as the Location to save this project, then click the OK button to create this new project. Perform the following opera- tions to change the properties of this new project: 1. Change the fi le object ’ s name from Form1.cs to EDModel Form.cs. 2. Change the Windows Form object ’ s name from Form1 to EDModelForm. 3. Change the content of the Text property of the Windows Form object from Form1 to Entity Data Model Form. 4. Change the StartPosition property of the form window to CenterScreen. 5. Add a Button control to the form window and name this button as cmdShow and set its Text property to ‘ Show Faculty ’ . Set its Font property to Bold — 12. 6. Add a Listbox control to the form window and name it as FacultyList. Set its Font property to Bold — 10. Your fi nished EDModelForm window should match the one shown in Figure 3.21 . c03.indd 132c03.indd 132 2/11/2010 11:51:01 AM2/11/2010 11:51:01 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... databases, such as Microsoft Access, Microsoft SQL Server, and Oracle databases In most cases, you should use the matched version of the Data Provider for a specific database Even the OLE DB and ODBC can work for that kind of database since the former can provide more efficient processing technique and faster accessing and manipulating speed compared with the latter To access and manipulate data in databases,... projection, aggregation, sorting, and more Practical Database Programming With Visual C#.NET, by Ying Bai Copyright © 2010 the Institute of Electrical and Electronics Engineers, Inc 147 148 Chapter 4 Introduction to Language-Integrated Query (LINQ) All SQO methods are located at the namespace System.Linq To use these methods, one must declare this namespace with a directive like: using System.Linq in... Access, SQL databases, XML documents, various Web services, and Oracle databases LINQ makes a query as a first-class language construct in C# and Visual Basic You write queries about strongly typed collections of objects by using language keywords and familiar operators In Visual Studio.NET you can write LINQ queries in C# with SQL Server databases, XML documents, ADO.NET DataSets, and any collection of... Tools to develop professional data-driven applications in Visual C# 2008 environment In Chapter 4, we will discuss a new technique, Language Integrated Query (LINQ), that was released with ADO.NET 3.5 and NET Framework 3.5 in Visual Studio.NET 2008 With the help of this new technique, the operational process of the data queries and manipulations with different data sources can be significantly simplified... match the one shown in Figure 3.25 Click on the Add button to add this component to the new project 3 The EDM Wizard is opened with two options: Generate from database and Empty model Select the first item Generate from database since we want to create this EDM from our sample database CSE_DEPT Click on the Next button to continue 3.4 Components of ADO.NET 2.0 Figure 3.24 Last dialog box for installation... groundbreaking innovation in Visual Studio 2008 and the NET Framework version 3.5 that bridges the gap between the world of objects and the world of data Traditionally, queries about data are expressed as simple strings without type checking at compile time or IntelliSense support Furthermore, you have to learn a different query language for each type of data source: Microsoft Access, SQL databases, XML documents,... the connection string, which has a different format and style depending on the database you are using The popular components of the connection string include Provider, Data Source, Database, User ID, and Password But some connection strings only use a limited number of components, such as the Data Provider for the Oracle database An important point in using the Command object to access and manipulate... again with all the settings we have created in the previous steps, which is shown in Figure 3.28 10 Make sure that Save entity connection settings in App.Config is checked since we need this connection string when we access our database as the project runs Also change this connection string to EDModelEntitiesConnString as shown in Figure 3.28 Click on the Next button to continue 11 The Choose Your Database. .. IEnumerable sequence Figure 4.3 shows an example of using the IQueryable interface to perform a query to a sample database CSE_DEPT A database connection has been made using the DataContext object before this piece of codes can be executed The LogIn is the name of a table in this sample database, and it has been converted to an entity before the LINQ query can be performed An IQueryable interface,... SequentialAccess mode; therefore, all data are read out and stored in a collection or an array in the EntityDataReader In Visual C#, a square bracket is used to indicate each element in a collection or an array Also since we created a nontyped DataSet, each column or entity property must be clearly indicated with the name of the column or the entity I Finally the connection is closed to release the connection object . Let ’ s begin with creating a new Visual C# Windows - based project named EDModel. 3.4.8.2.1 Create New Visual C# Windows -Based Project Open Visual Studio. NET. to the same project, with each item containing fi les that were generated from a different database and/or tables within the same database. c03.indd 129c03.indd

Ngày đăng: 26/01/2014, 08:20

Từ khóa liên quan

Mục lục

  • Practical Database Programming With Visual C#.NET

    • Contents

    • Preface

    • Acknowledgment

    • 1 Introduction

      • Outstanding Features of the Book

      • Target Audience

      • Topics Covered

      • Organization of the Book and How to Use It

      • How to Use the Source Code and Sample Databases

      • Instructor and Customer Support

      • Homework Solutions

      • 2 Introduction to Databases

        • 2.1 What Are Databases and Database Programs?

          • 2.1.1 File Processing System

          • 2.1.2 Integrated Databases

          • 2.2 Develop a Database

          • 2.3 Sample Database

            • 2.3.1 Relational Data Model

            • 2.3.2 Entity-Relationship Model (ER)

            • 2.4 Identifying Keys

              • 2.4.1 Primary Key and Entity Integrity

              • 2.4.2 Candidate Key

              • 2.4.3 Foreign Keys and Referential Integrity

              • 2.5 Define Relationships

                • 2.5.1 Connectivity

                • 2.6 ER Notation

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

Tài liệu liên quan