Advanced LINQ to SQL features in C#.NET

Creator QR-Code in C#.NET Advanced LINQ to SQL features

Advanced LINQ to SQL features
QR Code JIS X 0510 Drawer In Visual C#.NET
Using Barcode creation for Visual Studio .NET Control to generate, create QR Code JIS X 0510 image in .NET applications.
www.OnBarcode.com
QR Code JIS X 0510 Recognizer In Visual C#.NET
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Interfaces return as a result of ExecuteMethodCall
Code 39 Full ASCII Generator In Visual C#
Using Barcode generation for Visual Studio .NET Control to generate, create Code39 image in VS .NET applications.
www.OnBarcode.com
Generate Barcode In C#
Using Barcode encoder for Visual Studio .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
IExecuteResult exposes a ReturnValue of type Object and the ability to access parameter values to the parameters. If the procedure returns a list of objects that we can strongly type, we ll typically cast the ReturnValue as an ISingleResult<T> E. If it can return different types based on internal processing, we would use the IMultipleResults implementation, which allows us to access a specific type via the generic GetResult<TElement> method. Retrieving data via stored procedures is not limited to returning tables and result sets. They can just as easily return scalar values. Listing 8.13 demonstrates consuming the BookCountForPublisher stored procedure to return the count of the books for a given publisher. We don t return a result set, but rather rely on the return parameter, which contains the resulting count. As in the previous example, we call the procedure using the ExecuteMethodCall method of the DataContext.
Creating UPC-A Supplement 2 In C#
Using Barcode generation for VS .NET Control to generate, create UPC Symbol image in .NET applications.
www.OnBarcode.com
EAN-13 Supplement 5 Encoder In C#.NET
Using Barcode printer for .NET framework Control to generate, create EAN 13 image in .NET framework applications.
www.OnBarcode.com
Listing 8.13 Returning a scalar value
Printing Code-128 In C#
Using Barcode creation for Visual Studio .NET Control to generate, create Code 128 Code Set B image in VS .NET applications.
www.OnBarcode.com
UPC-E Supplement 5 Printer In C#
Using Barcode generator for .NET framework Control to generate, create UCC - 12 image in .NET framework applications.
www.OnBarcode.com
[Function(Name="dbo.BookCountForPublisher")] public int BookCountForPublisher( [Parameter(Name="PublisherId", DbType="UniqueIdentifier")] System.Nullable<System.Guid> publisherId) { IExecuteResult result = this.ExecuteMethodCall( this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), publisherId); return ((int)(result.ReturnValue)); }
QR Code 2d Barcode Recognizer In Visual Studio .NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
QR Code ISO/IEC18004 Printer In Java
Using Barcode creator for Java Control to generate, create QR image in Java applications.
www.OnBarcode.com
Advanced database capabilities
EAN / UCC - 13 Drawer In Java
Using Barcode creation for Eclipse BIRT Control to generate, create EAN 13 image in BIRT applications.
www.OnBarcode.com
Code 3/9 Decoder In Visual C#.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
In this case, we retrieve our scalar value through the return parameter of our stored procedure. LINQ to SQL realizes that there are no result sets being sent back. Instead, it presents the value that the stored procedure returns as the ReturnValue of the result. All we need to do is cast the value to the appropriate type. The ReturnValue will change depending on what kinds of data the procedure returns. In listing 8.14, we consume this method as we would any other method.
Encoding Code 128 Code Set C In Java
Using Barcode drawer for BIRT Control to generate, create USS Code 128 image in BIRT applications.
www.OnBarcode.com
Making PDF 417 In VB.NET
Using Barcode creation for .NET framework Control to generate, create PDF-417 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Listing 8.14 Consuming a scalar stored procedure
PDF 417 Generator In None
Using Barcode generation for Font Control to generate, create PDF 417 image in Font applications.
www.OnBarcode.com
Print Matrix In Java
Using Barcode drawer for Java Control to generate, create 2D Barcode image in Java applications.
www.OnBarcode.com
Guid publisherId = new Guid("851e3294-145d-4fff-a190-3cab7aa95f76"); Ch8DataContext context = new Ch8DataContext(); Console.WriteLine(String.Format("Books found: {0}", context.BookCountForPublisher(publisherId).ToString()));
Generating ANSI/AIM Code 39 In Java
Using Barcode creation for Java Control to generate, create Code 39 image in Java applications.
www.OnBarcode.com
UCC-128 Printer In None
Using Barcode generation for Word Control to generate, create UCC-128 image in Office Word applications.
www.OnBarcode.com
The fetching examples shown here are admittedly rudimentary. In actuality, we could perform the same queries using standard LINQ to SQL and avoid the need to define our own custom stored procedures. Depending on the application s business needs, directly accessing the table may be fine. Other business situations require the use of stored procedures for accessing data to meet security, performance, or auditing needs or to otherwise perform more advanced server-side processing before returning the results. Few changes are necessary to call stored procedures for these operations. Updating data with stored procedures Updating data is perhaps a more frequent use of stored procedures. Because a user making changes to data often requires more complex logic, security, or auditing, applications frequently rely on stored procedures to handle the remainder of the CRUD operations. As we ve observed already, LINQ to SQL covers some of the same concerns that lead many applications to use stored procedures. In many cases, the dynamic SQL created by calling SubmitChanges on the DataContext is sufficient. In other cases, using stored procedures is still necessary. If the use of stored procedures for updating data is required, using it is almost as easy as fetching records. However, we ll no longer be able to take advantage of the dynamic optimization that LINQ provides by only updating changed columns. Additionally, we ll be responsible for handling concurrency conflicts explicitly. Let s take look at what we need to do to replace the standard LINQ update method for our Author class. In listing 8.15 we see the definition of the stored procedure we can create to handle the update for the Author class. To illustrate how we can add additional
QR Code Maker In .NET
Using Barcode encoder for Reporting Service Control to generate, create Quick Response Code image in Reporting Service applications.
www.OnBarcode.com
Draw Barcode In None
Using Barcode maker for Word Control to generate, create Barcode image in Microsoft Word applications.
www.OnBarcode.com
Advanced LINQ to SQL features
functionality using stored procedures, this procedure will not only update the Author table, but also insert a record into the AuditTracking table.
Listing 8.15 Stored procedure to update an Author
CREATE PROCEDURE [dbo].[UpdateAuthor] @ID UniqueIdentifier output, @LastName varchar(50), @FirstName varchar(50), @WebSite varchar(200), @UserName varchar(50), @TimeStamp timestamp AS
DECLARE @RecordsUpdated int -- Save values UPDATE dbo.Author SET LastName=@LastName, FirstName=@FirstName, WebSite=@WebSite WHERE ID=@ID AND [TimeStamp]=@TimeStamp
SELECT @RecordsUpdated=@@RowCount IF @RecordsUpdated = 1 BEGIN
-- Add auditing record INSERT INTO dbo.AuditTracking (TableName, UserName, AccessDate) VALUES ('Author', @UserName, GetDate()) END RETURN @RecordsUpdated
This script is fairly standard. In it, we define a parameter for each value we re going to update B. Since we re using a timestamp column C, we don t need to send the original values for each column as well. This will help to optimize our network bandwidth to the server. We also declare an internal parameter called @RecordsUpdated that will help us track whether records were updated D. If no records are updated, we ll assume that there is a concurrency problem. Once we set up our values, we can try to call the update method E. Immediately after calling the update, we need to get the number of rows that were changed F. If we wait, @@RowCount will not return a reliable result.
Copyright © OnBarcode.com . All rights reserved.