SideBar    Getting Started with LINQ , LINQ FAQs
DOWNLOAD THE CODE:
Download the Code 48759.zip

Microsoft's new Language Integrated Query (LINQ—pronounced link) project is the next step in database-development technology. LINQ addresses the current database-development model's disconnect between the object-oriented programming model and procedural, T-SQL?based data-access code. Today, database-application developers use an object-oriented language such as C# or Visual Basic.NET to develop applications. ADO.NET serves as the object-oriented data-access middleware that connects the application with the database. However, even though the language and the data-access technology are all object oriented, developers still typically write data-access code in procedural T-SQL. For data access, ADO.NET provides an object-oriented wrapper around the TSQL code that the developer writes. This development paradigm requires developers to know not only the object-oriented .NET language but also T-SQL, and it forces developers to use two technologies to develop applications. Although Visual Studio's IntelliSense and code-completion features can help developers write correct .NET code, the IDE provides no design help with the T-SQL part of the development. For example, the IDE can't show the available databases and doesn't prompt the developer for the available tables or columns. Further, the only time the IDE even knows whether the query has been built with the correct syntax is at runtime, when a syntax error generates an exception.

LINQ technology addresses this problem through a set of .NET language extensions that let database developers write database queries and updates in either Visual Basic (VB) or C# without requiring them to drop back to T-SQL to write their data-access code. LINQ lets query expressions benefit from the rich metadata, compile-time syntax checking, type checking, and IntelliSense that until now were available only to native .NET code. LINQ has two implementations: one for XML (XLinq) and the other for databases (DLinq). XLinq enhances the .NET language with Xpath and XQuery functionality, and DLinq enhances the host .NET language with SQL-like query-expression statements. In this article, I give you a preview of the new DLinq technology you'll see in the beta and show you some early DLinq code that can connect to SQL Server to query and update a SQL Server database.

The Blue Pill
In the movie "The Matrix," characters swallowed a blue pill that altered their perception of reality. Although LINQ technology will change the way you think about developing applications for the database world, you don't need to swallow a blue pill.You just need to download the prerelease code from Microsoft. For details about obtaining LINQ, see the sidebar "Getting Started with LINQ." Once you've installed LINQ support, you can create a LINQ project by starting Visual Studio 2005, then selecting the File, New, Project option to display a New Project dialog box like the one that Figure 1 shows.

As you can see in Figure 1, the early LINQ release supports building console applications, Windows applications, LINQ libraries, or WinFX applications for Vista. To build a Windows application, select the LINQ Windows Application template from the Visual Studio New Project dialog box, give the project a name, and click OK to generate a starter project. The LINQ Windows Application template adds a reference to the System.Data.DLinq assembly and the System.XML.XLinq assembly. In addition, the template adds declarations for the XLinq and DLinq namespaces. For answers to some basic LINQ questions, including availability, see the sidebar "LINQ FAQs."

Connect
DLinq is a layer on top of ADO.NET that lets you freely mix DLinq code with ADO.NET code. To create a connection to a SQL Server database, you can instantiate an ADO.NET Connection object, then pass that connection object to the DLinq Data-Context object—thus connecting your DLinq application to SQL Server. All the code that uses the DLinq DataContext will in turn use the underlying ADO.NET connection.

The C# code in Listing 1 shows an example that connects DLinq to SQL Server through ADO.NET. At the top of this listing, you can see import declarations for the two LINQ namespaces and the declaration for the SQL Server .NET Data Provider. The code inside the DLinqConnect function connects to SQL Server by first creating an ADO.NET Connection object named cn, then passes that Connection object to the constructor of the DLinq DataContext.

Query
The really interesting parts of DLinq come out when you begin to use it for database access. DLinq relies on a database-to-object mapping that you must set up in your program.You can perform this mapping manually for simple one-off tables; however, manual mapping would be too tedious for most databases. To ease the process, Microsoft includes the SQLMetal utility as a part of the LINQ downloadable code. SQLMetal is a command-line utility that reads the contents of a database and creates an object-mapping file for your DLinq application. To use SQLMetal to create a class file containing the database mappings for the sample AdventureWorks database, use the following line:

C:\Program Files\LINQ 
  Preview\Bin>SqlMetal
  /server:sql2005
  /database:AdventureWorks
  /namespace:Adventureworks
  /code:AdventureWorks.cs 

Although the entire contents of the AdventureWorks.cs file are too big to list, you can get a taste for the relational object mapping that DLinq uses by looking at the code in Listing 2, which shows part of the mapping for the Department table. The Table decoration at the top of this listing identifies the name of the table in the target database as HumanResources.Department, and the class name that the DLinq application code uses is Department. Next, the code declares four private member variables for internal storage of the column variables. Then, you see a Column decoration for each column in the table. This code snippet shows the Column decorations for only the first two columns, DeaprtmentID and Name. For each column, the code defines the data type along with get and set methods that access the column's data values.

After you create the object mapping, you're ready to use DLinq to query the database. You can add the Adventure-Works.cs class file to your project by using the Project, Add Existing Item option from the Visual Studio menu.You can add a using statement (e.g., using Adventureworks;) to your other class files that need to access the database.

Now, you're ready to run DLinq queries that access the AdventureWorks database. Listing 3, shows a simple query that returns the contents of the HumanResources.Department table in the AdventureWorks database. Here, the DataContext object uses a connection string to connect to the SQL Server AdventureWorks database. This connection string is the same as a standard ADO .NET connection string. The DataContext object is responsible for translating the DLinq query operators to SQL, which the DLinq code will send to the target database.

   Prev. page   [1] 2     next page



You must log on before posting a comment.

If you don't have a username & password, please register now.

Reader Comments

See http://donxml.com/allthingstechie/archive/2006/02/06/2522.aspx

SQLDiva

SQLDiva

Article Rating 3 out of 5

Um, please do not confuse LINQ with DLINQ, as they are separate things. My whole rant is up on my blog, http://donxml.com/allthingstechie/archive/2006/02/06/2522.aspx but here is the jist of it:

LINQ is defined as "The LINQ Project is a codename for a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities."

LINQ, by itself, has nothing to do with database development technology, but with integrated language query. The query extensions are over any .NET array or collection, not databases. The DLINQ component of the LINQ Project, provides a run-time infrastructure for managing relational data as objects without giving up the ability to query. XLINQ does the same thing, but for XML.

DonXML

Article Rating 1 out of 5