Dienstag, 4. Mai 2010

LinqToERP: LINQ to SAP with ERPConnect and Visual Studio 2010

ERPConnect 4.0 has been released. This new Release contains a LINQ to SAP Provider for Visual Studio 2010.

Afther the installation of ERPConnect you must start the program LinqToERPSetupVS2010.exe from the installation folder (C:\Program Files (x86)\ERPConnect) to install LinqToERP.



In Visual Studio create new Project and add New Item. Select the template LINQ to ERP.


This add the file LINQtoERP1.erp to your Solution. Double-Click the file to open the designer.



As you see in the toolbox, LINQ to SAP supports following SAP Object types: Table (and View), Query, IDoc, BAPI and RFC (Function), InfoCube and InfoQuery (BWCube).

Select the table component from the toolbox. Drag and drop it to the designer. The connection dialog opens. Fill in the logon credential and host information and click ok.



The search table dialog opens. Search for a table and select it from the list.




In our sample we add the following 3 Tables:

KNA1: General Customer Data

VBAK: Header data of Sales Documents

VBAP: Item data of Sales Documents



In this blog we create a sample with three master/details Datagridviews with the Tables Customers, SalesHeaders and SalesItems.


create a DataSet

using (SAPContext sc = new SAPContext(username, password))

{

DataSet ds = new DataSet("LinqToSAP");

ds.Locale = System.Globalization.CultureInfo.InvariantCulture;

Here we have used an extension method to convert the Linq Expression to a DataTable. For more Information see the following site: http://weblogs.asp.net/stevesloka/archive/2008/01/28/linq-to-datatable.aspx

Create a customers table and add it to the DataSet

var customerQuery = from c in sc.KNA1List

where c.LAND1 == "DE"

select c;

DataTable customerTable = customerQuery.ToADOTable(rec => new object[] { customerQuery });

customerTable.TableName = "Customers";

ds.Tables.Add(customerTable);

Create a salesHeader Table and add it to the DataSet

var salesHeaderQuery = from s in sc.VBAKList

where s.WAERK == "DEM" || s.WAERK == "EUR"

select s;

DataTable salesHeaderTable = salesHeaderQuery.ToADOTable(rec => new object[] { salesHeaderQuery });

salesHeaderTable.TableName = "SalesHeader";

ds.Tables.Add(salesHeaderTable);

Create a Relation between Customers and SalesHeader

DataRelation rel = new DataRelation(

"CustomerSalesRelation",

ds.Tables["Customers"].Columns["KUNNR"],

ds.Tables["SalesHeader"].Columns["KUNNR"],false);

ds.Relations.Add(rel);

Create a SalesItems Table and add it to the dataset

var salesItemsQuery = from i in sc.VBAPList

where i.WAERK == "DEM" || i.WAERK == "EUR"

select i;

DataTable salesItemsTable = salesItemsQuery.ToADOTable(rec => new object[] { salesItemsQuery });

salesItemsTable.TableName = "SalesItems";

ds.Tables.Add(salesItemsTable);

Create a Relation between SalesHeader and SalesItems

DataRelation rel2 = new DataRelation(

"SalesItemsRelation",

ds.Tables["SalesHeader"].Columns["VBELN"],

ds.Tables["SalesItems"].Columns["VBELN"], false);

ds.Relations.Add(rel2);

Define 3 Datagridviews with the corresponding Bindingsources. Define DataSource and DataMember for each BindingSource.

customerbs.DataSource = ds;

customerbs.DataMember = "Customers";

salesHeaderbs.DataSource = customerbs;

salesHeaderbs.DataMember = "CustomerSalesRelation";

salesItemsbs.DataSource = salesHeaderbs;

salesItemsbs.DataMember = "SalesItemsRelation";

Now we have developed a sample with SAP to LINQ in 10 minutes.