CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Accessing data using Language Integrated Query (LINQ) in ASP.NET WebPages – Part 2

By Balamurali Balaji
Posted On Feb 27,2008
Article Rating:
Be first to rate
this article.
No of Comments: 5
Category: ASP.Net
Print this article.

Accessing data using Language Integrated Query (LINQ) in ASP.NET WebPages – Part 2

 

Part 2, sequel and the last of the article Accessing data using Language Integrated Query(LINQ) in ASP.NET WebPages - Part 1 explains how to create entity classes to represent SQL Server database and tables using Object Relational Designer and display data in a web page using LinqDataSource control.

 

LinqDataSource Control

Using declarative markup, you can create a LinqDataSource control that connects to data from a SQL database table, and configure it to handle updating, inserting, and deleting data without having to write the SQL commands to perform these tasks. The LinqDataSource class also provides an event model that enables you to handle customized scenarios.

To use LinqDataSource control to query a database, entity objects must be created to represent the database and its tables. You can use the Object Relational Designer or the SqlMetal.exe utility to generate these objects.The ContextTypeName property must be set to the object that represents the database and the TableName property to the object that represents the database table.

 

Object Relational Designer

The Object Relational Designer (O/R Designer) provides a visual design surface for creating LINQ to SQL entity classes and associations (relationships) based on objects in a database. In other words, the O/R Designer is used to create an object model in an application that maps to objects in a database. The O/R Designer also provides functionality to map stored procedures and functions to DataContext methods for returning data and hydrating entity classes. The O/R Designer also provides the ability to design inheritance relationships between entity classes. The O/R Designer currently supports only SQL Server 2000 and later, and SQL Server Express databases

The O/R Designer has two distinct areas on its design surface: the entities pane on the left, and the methods pane on the right. The entities pane is the main design surface that displays the entity classes defined in the file. The methods pane is the design surface that displays the DataContext methods that are mapped to stored procedures and functions. You can hide the methods pane. Using methods pane is out of scope of this article.

 

Code Generation tool (SqlMetal.Exe)

The SqlMetal command-line tool is used to generate code and mapping for the LINQ to SQL component of the .NET Framework. By applying options as listed below, you can instruct SqlMetal to perform a number of different actions, including the following:

From a database, generate source code and mapping attributes or a mapping file.
From a database, generate an intermediate DBML file for customization.
From a DBML file, generate code and mapping attributes or a mapping file.
The file is located by default at [drive:]\program files\microsoft sdks\windows\v6.0A\bin.

 

>sqlmetal [options] [<input file>]

 

Example 1: To generate a .dbml file with extracted SQL metadata, issue the following command in the Visual Studio Command Prompt window.

 

>sqlmetal /server:myserver /database:northwind /dbml:mymeta.dbml

 

Example 2: To generate a .dbml file with extracted SQL metadata from an .mdf file with SQL Server Express, issue the following command

 

>sqlmetal /dbml:mymeta.dbml mydbfile.mdf

 

Walkthrough 2: Connecting Sql Server Express Database using O/R Designer and LinqDataSource and perform queries on a table to display data in a ASP.Net webpage

Add a new Webpage(Default2.aspx) to the existing WebSite project you have just created to test the sample Walthrough 1.

In the WebSite Menu, select “Add New Item” command to choose the “Linq to Sql Classes” from the available list of templates in the dialog. It creates a new entity class’s file called DataClasses.dbml in the website’s App_Code folder.

After you add the new LINQ to SQL file to the project, the empty design surface represents a DataContext ready to be configured. The DataContext name corresponds to the name that you provided for the .dbml file. For this walkthrough, because you named the LINQ to SQL file DataClasses.dbml, the DataContext is named DataClassesDataContext. You can verify this by clicking an empty area on the designer and inspecting the Properties window.

Next step is to create LINQ to SQL classes that are mapped to database tables by dragging tables from Server Explorer/Database Explorer onto the O/R Designer. The result is a LINQ to SQL entity class that maps to the table in the database. (Pic5)

linqdatapic5.jpg

In Server Explorer/Database Explorer, locate the tables in the SQL Server version of the sales.dbf sample database used in this article.

Drag the Product node from Server Explorer/Database Explorer onto the O/R Designer surface.

An entity class named Product is created to represent a single product from the Product table. It has properties that correspond to the columns in the Product table.




Drag a LinqDataSource control(ID=LinqDataSource1) into Default2.aspx page from the data tab in the toolbox and configure it to access the data sources and queries you have just created in the project as below in the Configure Data Source Wizard.

1. Choose “DataClassDataContext” as a Context object. (Pic6)

linqdatapic6.jpg

2. Choose “products” as Table in the Data Selection and Check all the fields appear in the Select list.

3. Click the “Finish” button to complete the configuration of LinqDataSource control.

Drag a GridView control(ID=GridView1) into the page from the data tab in the tool box and set Data Source property to LinqDataSource1.

That’s’ it. Now run the webpage and see the output as displayed below. (pic7)

linqdatapic7.jpg

Conclusion:

This two-part article explained basically how to use LinqDataSource control available with the new version of ASP.NET in Visual Studio 2008 to connect to two different data sources namely Object data collection and Sql Server Express Database table. It also detailed how to define object and entity classes and execute queries to display data. In coming articles, I would explore more on using Linq with other data sources as well.

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
???? Win7???32??????
???? Win7???32?????? Error executing SQL.Error [26]: File oepend that is not a database file."SELECT * FROM [UpFileHis]": file is encrypted or is not a database.Access violation at address 00616755 in module 'UDown.exe'. Read of address 00000008. http://wqsskmas.com [url=http://mdydiufj.com]mdydiufj[/url] [link=http://lyypvvwgrsk.com]lyypvvwgrsk[/link]
???? Win7????? 64???
???? Win7????? 64?????? Error executing SQL.Error [26]: File<a href="http://hfyardnxdbj.com"> opeend</a> that is not a database file."SELECT * FROM [UpFileHis]": file is encrypted or is not a database.Access violation at address 00616755 in module 'UDown.exe'. Read of address 00000008.
???? Windows VISTA E
???? Windows VISTA Enterprise sp2 x64???? Error executing SQL.Error [26]: File onpeed that is not a database file."SELECT * FROM [UpFileHis]": file is encrypted or is not a database.Access violation at address 00616755 in module 'UDown.exe'. Read of address 00000008. http://mwiqbrg.com [url=http://eykgiziix.com]eykgiziix[/url] [link=http://jqupahjikub.com]jqupahjikub[/link]
???? winxp sp3 (?win
???? winxp sp3 (?win 7 64bit?VM)???? Error executing SQL.Error [26]: File <a href="http://tydlqnmlx.com">opneed</a> that is not a database file."SELECT * FROM [UpFileHis]": file is encrypted or is not a database.Access violation at address 00616755 in module 'UDown.exe'. Read of address 00000008.???????, ProgID: "transfer.Config".
???? Windows VISTA H
???? Windows VISTA Home Premium sp2 x32???? Error executing SQL.Error [26]: File opeend that is not a database file."SELECT * FROM [UpFileHis]": file is encrypted or is not a database.Access violation at address 00616755 in module 'UDown.exe'. Read of address 00000008.???RJ?????"??C:\ProgramData\115\UDown\????? Account.ini?? ? Data??? (?????) ??????115???????"??????^^PS.????????? ????? Data??? ???????