DBRestToTable Windows Workflow Activity

Activity , Rest. . ctivity c . , , , . , Activity ActivityRest. Entity, , Linq. , . MS Visual Studio 2010 WCF + WorkFlow + REST + WindowsFormClient, Activity.
1. DBRestToTable.
Activity ActivityRest, InputArgument :
public class InputArgument
{
public string UrlString{ get; set; }
public string CustomerID{ get; set; }
}
2. DBRestToTable.
, ActivityRest, . , , , protected override EndExecute , IEnumerable<Order>.
protected override IEnumerable<Order> EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
{
try
{
3. DBRestToTable.
Activity , , , , . >
/* 25.01.2013.
Activity , , Rest
, , Microsoft SQL
.
Northwind Entity,
Linq.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using WorkFlowRest.ServiceReference1;
using System.Data.Services.Client;
using ClassLibrary;
using System.ComponentModel;

namespace WorkFlowRest
{
public sealed class DBRestToTable : AsyncCodeActivity<IEnumerable<Order>>
{
[RequiredArgument]
[DefaultValue(null)]
public InArgument<InputArgument> inputArguments { get; set; }

NorthwindEntities entityContext= null;
DataServiceQuery<Order> entityQuery= null;

// Temporary variable for input arguments
private InputArgument inputObject= null;
private string uri= null;
private string customerId= null;

protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
{
try
{
// Input Object
inputObject = inputArguments.Get(context);

//Uri for service
uri = inputObject.UrlString;
if (String.IsNullOrEmpty(uri)) throw new ArgumentNullException("Value", "Uri string is Empty");

// Filter for Data
customerId = inputObject.CustomerID;
if (customerId == null) throw new ArgumentNullException("Value", "CustomerID string is null");

// Set EntityContext
entityContext = new NorthwindEntities(new Uri(uri));

// Create entityQuery
entityQuery = (DataServiceQuery<Order>)
from obj in entityContext.Orders
where obj.Customer.CustomerID.Contains(customerId)
select obj;

// Set context in Local DataBase
DataServerDataContext db = new DataServerDataContext();

// Create table Temp
if (db.CreateTable() == -1) throw new ArgumentException("Value", "Error Create table Temp.. ");

db.SubmitChanges();

// Insert records in table
foreach (Order item in entityQuery)
{
if(db.InsertTableTemp(item.OrderID,
item.CustomerID,item.EmployeeID,item.OrderDate,
item.RequiredDate,item.ShippedDate,item.ShipVia,item.Freight,item.ShipName,
item.ShipAddress,item.ShipCity,item.ShipRegion,item.ShipPostalCode, item.ShipCountry) == -1)
throw new ArgumentException("Value", "Error insert in table Temp.. ");

// Debug Activity
//System.Diagnostics.Debug.WriteLine(item.OrderID.ToString());
}
db.SubmitChanges();

context.UserState = entityQuery;
return entityQuery.BeginExecute(callback, state);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
return null;
}
}

protected override IEnumerable<Order> EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
{
try
{
// Get context.UserState
entityQuery = context.UserState as DataServiceQuery<Order>;
// Return entityQuery
return entityQuery.EndExecute(result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
return null;
}
}
}
}
4. DBRestToTable
, URI DBRestToTable InputArgument. , , , Linq . CustomerID. Linq ComboBox .>
/* 25.01.2013. DBRestToTable.
Uri .
. , Linq
( DBRestToTable).
Linq
DataSource ComboBox.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsClient.ServiceReference2;
using ClassLibrary;

namespace WindowsFormsClient
{
public partial class FormClient : Form
{
// Link for Service Client
ServiceClient client = null;
// Input Class
private ClassLibrary.InputArgument input = null;

public FormClient()
{
InitializeComponent();
// Create Objects and set parameters
client = new ServiceClient();
input = new InputArgument();
input.CustomerID = string.Empty;
input.UrlString = @"http://services.odata.org/Northwind/Northwind.svc/";
}

// Event click button for clean filter
private void button1_Click(object sender, EventArgs e)
{
// Empty Filter comboBox1
comboBox1.Text = "";
input.CustomerID = string.Empty;
//Call Form1_Load
FormClient_Load(sender, e);
}

// Load Data
private void FormClient_Load(object sender, EventArgs e)
{
try
{
// Send and Get Data
var order = client.GetData(input);
// Too Query
var groupCustomerID = from row in order
// orderby row.CustomerID ascending
group row by row.CustomerID into grouping
orderby grouping.Key ascending
select grouping.Key;

foreach (var item in groupCustomerID) comboBox1.Items.Add(item.ToString());

// Exception is order == null
if (order == null) { throw new ArgumentNullException("Value", "Order is null"); }

// Set DataSource
dataGridView1.DataSource = order;
label3.Text = order.Count<Order>().ToString();
}
catch (Exception ex)
{
MessageBox.Show(" Error: " + ex.ToString());
}
}

// Change CustomerID in ComboBox
private void comboBox1_TextChanged(object sender, EventArgs e)
{
try
{
input.CustomerID = (string)comboBox1.SelectedItem;
// Send and Get Data
var order = client.GetData(input);
// Exception
if (order == null) { throw new ArgumentNullException("Value", "Order is null"); }
// Set DataSource
dataGridView1.DataSource = order;
label3.Text = order.Count<Order>().ToString();
}
catch (Exception ex)
{
MessageBox.Show(" Error: " + ex.ToString());
}
}
}
}
5. SQL .
, CreateTable InsertTableTemp. blog Scott Guthrie, InsertTableTemp.
-- Create a table or delete records to a table
ALTER PROCEDURE dbo.CreateTable
AS
BEGIN TRY
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Temp'))
-- if the table exists remove entries
BEGIN
DELETE FROM [DBO].[Temp]
RETURN 1
END
-- if the table does not exist create it
ELSE
BEGIN
CREATE TABLE [dbo].[Temp]
(
OrderIDint,
CustomerIDnchar(5),
EmployeeIDint,
OrderDatedatetime,
RequiredDatedatetime,
ShippedDatedatetime,
ShipViaint,
Freightmoney,
ShipNamenvarchar(40),
ShipAddressnvarchar(60),
ShipCytynvarchar(15),
ShipRegionnvarchar(15),
ShipPostalCodenvarchar(10),
ShipCountrynvarchar(15)
)
RETURN 2
END
END TRY
-- if Error return -1
BEGINCATCH
RETURN -1
ENDCATCH

-- INSERT RECORD IN TEMP
ALTER PROCEDURE dbo.InsertTableTemp
(
@OrderIDint,
@CustomerIDnchar(5),
@EmployeeIDint,
@OrderDatedatetime,
@RequiredDatedatetime,
@ShippedDatedatetime,
@ShipViaint,
@Freightmoney,
@ShipNamenvarchar(40),
@ShipAddressnvarchar(60),
@ShipCytynvarchar(15),
@ShipRegionnvarchar(15),
@ShipPostalCode nvarchar(10),
@ShipCountrynvarchar(15)
)

AS
BEGIN TRY
INSERT INTO [dbo].[Temp]
([OrderID],[CustomerID],[EmployeeID],[OrderDate],[RequiredDate],[ShippedDate],[ShipVia],[Freight],
[ShipName],[ShipAddress],[ShipCyty],[ShipRegion],[ShipPostalCode],[ShipCountry])
VALUES
(@orderID, @CustomerID,@EmployeeID,@OrderDate,@RequiredDate,@ShippedDate,@ShipVia, @Freight,@ShipName,
@ShipAddress, @ShipCyty,@ShipRegion, @ShipPostalCode,@ShipCountry)

SET @OrderID = CAST (SCOPE_IDENTITY() AS INT)
RETURN 1
END TRY
BEGIN CATCH
RETURN - 1
END CATCH

Visual Studio 2010 : Activity, Workflow, Windows Form .
C , .
25.01.2013 .

© Argument Ltd, 2017 ,  : 8-921-215-45-70,   e-mail