Microsoft ADO .NET 4 Step by Step - p 36 pps

10 100 0
Microsoft ADO .NET 4 Step by Step - p 36 pps

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

Thông tin tài liệu

326 Microsoft ADO.NET 4 Step by Step Calling Custom Database Functions: C# Note This exercise continues the previous exercise in this chapter. 1. Open the source code view for the StatesByYear form. This form will access the AdmittedInYear database function. Your sample database should already have this func- tion defined. Note The SalesOrder.edmx project file includes the SSDL definition of the function within the storage model layer. The code for both appears in the discussion immediately before this example. 2. Within the StatesByYear class, add the following static method: [EdmFunction("SalesOrderModel.Store", "AdmittedInYear")] public static int? AdmittedInYear(DateTime? whichDate) { // This is a stub for the true AdmittedInYear function // in the database. throw new NotSupportedException("Direct calls are not supported."); } This routine defines the function stub that both LINQ and Visual Studio’s IntelliSense will use to recognize the database function. This function cannot be called directly, as evidenced by the thrown exception within the function body. Instead, the EdmFunction attribute notifies LINQ that it should locate the Admi ttedInYear function exposed in the SalesOrderModel.Store namespace of the model and call that function instead. The date parameter and the return value for the stub are both nullable types because the query can pass NULL values to the actual database function. 3. Locate the StatesByYear_Load event handler; this is a routine that loads the data onto the form. Add the following code as the body of the routine: var result = from st in context.StateRegions orderby st.Admitted select new { StateName = st.FullName, AdmitYear = SqlFunctions.DatePart("year", st.Admitted), TotalAdmittedInYear = AdmittedInYear(st.Admitted) }; StateAdmittance.DataSource = result.ToList(); This query includes a call to both a native database function (SqlFunctions.Date Part) and the custom function (AdmittedInYear) that matches the function stub added in the previous step. When LINQ to Entities processes this query, it builds a SQL statement that includes a T-SQL call to the database-level Admitted InYear custom function. Chapter 19 Using LINQ to Entities 327 4. Run the program. When the Switchboard form appears, click States By Year. When the StatesByYear form appears, the query results will display in the form’s main grid. The TotalAdmittedInYear column displays an integer value generated by the logic in the custom database function. Calling Custom Database Functions: Visual Basic Note This exercise continues the previous exercise in this chapter. 1. Open the source code view for the StatesByYear form. This form will access the AdmittedInYear database function. Your sample database should already have this func- tion defined. Note The SalesOrder.edmx project file includes the SSDL definition of the function within the storage model layer. The code for both appears in the discussion immediately before this example. 2. Within the StatesByYear class, add the following shared method: <EdmFunction("SalesOrderModel.Store", "AdmittedInYear")> Public Shared Function AdmittedInYear( ByVal whichDate As Date?) As Integer? ' This is a stub for the true AdmittedInYear function ' in the database. Throw New NotSupportedException("Direct calls are not supported.") End Function 328 Microsoft ADO.NET 4 Step by Step This routine defines the function stub that both LINQ and Visual Studio’s IntelliSense will use to recognize the database function. This function cannot be called directly, as evidenced by the thrown exception within the function body. Instead, the EdmFunction attribute notifies LINQ that it should locate the AdmittedInYear function exposed in the SalesOrderModel.Store namespace of the model and call that function instead. The date parameter and the return value for the stub are both nullable types because the query can pass NULL values to the actual database function. 3. Locate the StatesByYear_Load event handler; this is a routine that loads the data onto the form. Add the following code as the body of the routine: Dim result = From st In context.StateRegions Select StateName = st.FullName, AdmitYear = SqlFunctions.DatePart("year", st.Admitted), TotalAdmittedInYear = AdmittedInYear(st.Admitted) Order By AdmitYear StateAdmittance.DataSource = result.ToList This query includes a call to both a native database function (SqlFunctions.DatePart) and the custom function (AdmittedInYear) that matches the function stub added in the previous step. When LINQ to Entities processes this query, it builds a SQL statement that includes a T-SQL call to the database-level AdmittedInYear custom function. 4. Run the program. When the Switchboard form appears, click States By Year. When the StatesByYear form appears, the query results will display in the form’s main grid. The TotalAdmittedInYear column displays an integer value generated by the logic in the custom database function. Chapter 19 Using LINQ to Entities 329 Summary This chapter introduced LINQ to Entities, a LINQ provider that allows you to run queries against entity types within a generated Entity Framework conceptual model. Because entities and their properties are strongly typed, it’s simple to include them in LINQ queries without additional data type conversion code. Also, the syntax used in the query is nearly identical to the basic LINQ to Objects form. If there is a downside to LINQ to Entities, it is its inability to include client-side content within the query. Because its queries are processed within the database using that platform’s native SQL language, locally defined logic and data content can’t always make the transition to the processing layer. Fortunately, LINQ to Entities exposes both predefined and custom database functions that let you move any needed logic to the database level. Chapter 19 Quick Reference To Do This Include an Entity Framework entity in a LINQ query Generate the entity container from the conceptual model, storage model, and mapping layer. Create an ObjectContext instance for the generated model. Use the context’s exposed entity collections within the From clauses of a LINQ query. Include a custom SQL Server function within a LINQ to Entities query Add the function to the storage layer of the Entity Framework model. Generate the entity container for the model that contains the function. Create a static (C#) or shared (Visual Basic) function stub that has the same name, arguments, and return value (or a reasonable .NET equivalent) as the original function. Call the function within a LINQ query. Chapter 20 Using LINQ to SQL After completing this chapter, you will be able to:  Build LINQ queries that use the LINQ to SQL provider  Understand how LINQ to SQL prepares queries for processing  Determine when to use LINQ to SQL over LINQ to Entities LINQ is an extensible system, enabling a consistent querying experience against different data platforms. Sometimes these different systems overlap, providing access to the same target platform through different providers. SQL Server is one such database platform. LINQ to DataSet and LINQ to Entities both allow you to build LINQ queries that interact with data sourced from SQL Server, either directly (LINQ to Entities) or indirectly (LINQ to DataSet). LINQ to SQL, also included as a native LINQ provider within the .NET Framework, provides a third option for processing SQL Server data. This chapter focuses on the LINQ to SQL provider and the benefits it supplies to your data- focused application. In many ways, LINQ to SQL feels like LINQ to Entities, especially when using its class-generation feature. However, LINQ to SQL was built specifically to interact with SQL Server database tables, and its queries reflect that closer relationship. Note In October 2008, soon after the announcement of its plans for Visual Studio 10 and the related .NET Framework 4.0 release, Microsoft provided guidance on the future of its LINQ to SQL provider. This “Update on LINQ to SQL and LINQ to Entities Roadmap” blog entry posted by the ADO.NET team (http:// blogs.msdn.com/b/ad onet/archive/2008/10/29/update-on-linq-to- sql-and-linq-to-enti ties-roadmap.aspx) indicated that the Entity Framework would be the “rec- ommended data access solution for LINQ to relational scenarios.” The posting also included a commitment to evolve the LINQ to SQL product based on customer feedback. Note The exercises in this chapter all use the same sample project, a tool that makes queries using LINQ to SQL. Although you will be able to run the application after each exercise, the ex- pected results for the full application might not appear until you complete all exercises in the chapter. 332 Understanding the LINQ to SQL Provider LINQ to SQL is a LINQ provider that targets SQL Server databases. Its simple configuration needs and its focus on the logical organization of the underlying tables make it a useful tool for applications that need easy access to a specific SQL Server database. Comparing LINQ to SQL with LINQ to Entities The LINQ to SQL provider first appeared with the initial release of LINQ, part of Visual Studio 2008 and the .NET Framework 3.5. It preceded the release of LINQ to Entities, which was delivered as part of the .NET Framework 3.5 SP1. The two platforms share many similarities, including the following:  Modeling of data entities using an XML-based schema language  A Visual Studio–hosted visual designer that simplifies XML model creation  Generation of a language-specific class layer from the model  Support for database updates using custom stored procedures  Delayed query processing through LINQ-constructed SQL statements Despite these similarities, the two systems diverge significantly in many respects. These dif- ferences often help determine which of the two solutions is best for a given application. The two systems differ in four key areas:  Platform support The Entity Framework and its LINQ to Entities extension include support for a wide range of database platforms. In contrast, LINQ to SQL communicates only with SQL Server 2000 and later, including SQL Server Compact 3.5. Note The visual designer used to set up LINQ to SQL models does not work with SQL Server Compact 3.5.  Model focus LINQ to Entities queries focus on the Entity Framework conceptual model, which is an abstraction of the underlying logical database model. This model can differ significantly from the organization presented within the database. In LINQ to SQL, the model closely reflects the database tables that support it.  Overhead LINQ to SQL is extremely lightweight compared with its Entity Framework counterpart. EF’s three model layers allow for tremendous flexibility, but such design comes at a performance and memory overhead cost.  Extensibility While the query features available in both LINQ to Entities and LINQ to SQL are comparable, the Entity Framework’s design offers many opportunities for third- party enhancement that aren’t currently possible with LINQ to SQL. Chapter 20 Using LINQ to SQL 333 Using LINQ to SQL, especially when building models with its visual designer, is straightforward and often much quicker than setting up a LINQ to Entities environment. Microsoft’s official encouragement to use the Entity Framework option may help guide your decision, but for applications that have simple needs and access to SQL Server data, LINQ to SQL may be the best query platform. Understanding the Components of LINQ to SQL The focus of an Entity Framework model is the XML-based definition of the three different layers: conceptual, storage, and mapping. While LINQ to SQL can use an XML definition as the basis for a specific data implementation, the focus of each table definition is the entity class, a standard .NET class decorated with attributes from the System.Data.Linq.Mapping namespace. C# [Table(Name="UnitOfMeasure")] public class UnitOfMeasure { // As defined in the database: // ID bigint // ShortName varchar(15) // FullName varchar(50) [Column(IsPrimaryKey = true)] public long ID; [Column] public string ShortName; [Column] public string FullName; } Visual Basic <Table(Name:="UnitOfMeasure")> Public Class UnitOfMeasure ' As defined in the database: ' ID bigint ' ShortName varchar(15) ' FullName varchar(50) <Column(IsPrimaryKey:=True)> Public ID As Long <Column> Public ShortName As String <Column> Public FullName As String End Class 334 Microsoft ADO.NET 4 Step by Step Attributes, such as the TableAttribute and ColumnAttrib ute shown in this code block, inform LINQ to SQL how to map class members to tables and columns in the database. Additional attributes identify storage-level data types, intertable relationships, stored procedure defini- tions, and other key items that let the application communicate cleanly with the external data source. The System.Data.Linq.DataContext class binds these class definitions with actual data and acts much like the ObjectContext class in the Entity Framework. Derived versions of DataContext include instances of the decorated classes, forming a class-style representation of the actual database. C# public class SalesOrderLink : DataContext { // Constructor establishes database connection. public SalesOrder(string connectionString): base(connectionString) {} // Table definitions to link with database. public Table<Customer> Customers; public Table<OrderEntry> Orders; public Table<UnitOfMeasure> UnitsOfMeasure; } Visual Basic Public Class SalesOrderLink Inherits DataContext ' Constructor establishes database connection. Public Sub New(ByVal connectionString As String) MyBase.New(connectionString) End Sub ' Table definitions to link with database. Public Customers As Table(Of Customer) Public Orders As Table(Of OrderEntry) Public UnitsOfMeasure As Table(Of UnitOfMeasure) End Class After you have a defined context, using it with LINQ is a simple matter of creating an in- stance of the context and adding its exposed members to a query. Chapter 20 Using LINQ to SQL 335 C# using (SalesOrderLink context = new SalesOrderLink(connectionString)) { var results = from cu in context.Customers orderby cu.FullName select new { CustomerID = cu.ID, CustomerName = cu.FullName }; } Visual Basic Using context As New SalesOrderLink(connectionString) Dim results = From cu In context.Customers Order By cu.FullName Select CustomerID = cu.ID, CustomerName = cu.FullName End Using Except for the replacement of an ObjectContext by a DataContext, this query is identical to the first LINQ to Entities query included in Chapter 19, “Using LINQ to Entities.” Like its Entity Framework complement, LINQ to SQL uses the clauses in the query (in either the standard LINQ form shown here or one built with extension methods) to craft SQL state- ments that retrieve, project, filter, and sort the data returned by the query. Because of this, you are limited in the types of non-LINQ-To-SQL data that you can include in the query. Only data content and functionality that can be represented easily in a SQL statement are candi- dates for inclusion in a LINQ to SQL query. Despite this limitation, LINQ to SQL does a pretty good job at converting ordinary .NET ele- ments into SQL equivalents. Comparisons with null (C#) and Nothing (Visual Basic) translate into the expected IS NULL and IS NOT NULL forms. Using the Visual Basic Like pattern- matching operator results in a similar LIKE comparison in the generated SQL. Including the AddDays method on a date value within a LINQ to SQL query properly converts the expres- sion into one that uses the related DATEADD function in T-SQL. For a complete list of all .NET features that LINQ to SQL can use in database queries, see the “Data Types and Functions (LINQ to SQL)” entry in the Visual Studio online help. Using the Object Relational Designer Although you can handcraft your own entity classes, a better option for large databases (and even small ones) is to use the Object Relational (O/R) Designer, a drag-and-drop visual de- signer that generates LINQ to SQL classes based on a graphical database model. To use the O/R Designer, add a new “LINQ to SQL Classes” item to your Visual Basic or C# project. . SQL provider. This “Update on LINQ to SQL and LINQ to Entities Roadmap” blog entry posted by the ADO. NET team (http:// blogs.msdn.com/b/ad onet/archive/2008/10/29/update-on-linq-to- sql-and-linq-to-enti. 326 Microsoft ADO. NET 4 Step by Step Calling Custom Database Functions: C# Note This exercise continues the previous exercise in this chapter. 1. Open the source code view for the StatesByYear. <Column> Public FullName As String End Class 3 34 Microsoft ADO. NET 4 Step by Step Attributes, such as the TableAttribute and ColumnAttrib ute shown in this code block, inform LINQ to SQL how to map

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

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

  • Đang cập nhật ...

Tài liệu liên quan