| |
KnowledgeBase for ActiveReports for .NET
02-06-2008, 12:27 PM
|
|
|
|
With the introduction of Visual Studio 2008, a new way of accessing and querying data has also been introduced. By utilizing LINQ (Language INtegrated Query), it is now even easier to retrieve data for your reports.
Attached is a sample program which uses a basic LINQ command to retrieve data from the NWIND database. To set up LINQ to SQL objects in your project, you will need to follow these steps:
• In your VS 2008 project, go to the View menu and click the Server Explorer menu item • On the treeview on the left, right-click the Data Connections and select ‘Add Connection’ • In the next dialog select ‘Microsoft SQL Server Database File’ and click Continue • In the next dialog, click the Browse button, and navigate to “C:\SQL Server 2000 Sample Databases”. Select the ‘NORTHWND.MDF’ file, and click Open • Click OK to finish Adding the Connection • In your Treeview node, under ‘Data Connections’ NORTHWIND.MDF will be available. Expand this node (click on the plus beside it) and expand the Tables node • Note: The steps up to this point need only be completed once. If you create a new project, then the database will still be available through server explorer. You may need to re-open the Server Explorer window from session to session (see below) • Now add a Linq To Sql file to your application. Go to the Project menu, and select ‘Add New Item’ • In the Add New Item dialog, select the ‘LINQ To SQL Classes’ item • Change the name to ‘Northwind.dbml’ and click Add • From the Server Explorer window (which will still be open) select the Products table from the node we expanded (DataConnections\NORTHWND.MDF\Tables) and drag it over to the O/R designer window to the right (this window will have opened after we added the Northwind.dbml file) • You will get a dialog asking if you wish to copy the database locally. Click Yes • Close the designer file, and the Server Explorer window. You’re ready to start writing your LinQ To Sql code! • To reopen the Server Explorer window, go to the View menu, and select Server Explorer • To reopen the O/R designer, go to the Solution Explorer window, and double-click Northwind.dbml
In this sample project, LINQ is used in the code behind of the ViewerForm. The code is as follows:
C#.NET:
NorthwindDataContext db = new NorthwindDataContext();
var datasource = from products in db.Products orderby products.SupplierID select products;
rpt.DataSource = datasource.ToList();
VB.NET
Dim db As New NorthwindDataContext
Dim datasource = From products In db.Products _ Order By products.SupplierID _ Select products
rpt.DataSource = datasource.ToList
In the above code, a new object of the LINQ data classes we created earlier is made. It is then used to execute the LINQ command which retrieves data from the database and stores it in the datasorce variable. We can then set the rpt.DataSource property to datasource.ToList(), which converts the datasource to an object the report can use as a datasource. The datasource.ToArray() method will also work correctly.
If you are interested in learning more about LINQ, refer to this MSDN article for more information: http://msdn2.microsoft.com/en-us/library/bb386976(VS.90).aspx
Also, if you do not have the Northwinds database for SQL Server, you can download it here: http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46A0-8DA2-EEBC53A68034&displaylang=en
Applies To: ActiveReports for .NET 3.0
|
|
|
|
|
|
GrapeCity » Knowledge Base » KnowledgeBase f... » Using ActiveReports with LINQ
|
|
|
|
|