WF动态挂单(1)
WF與其他工作流框架不同,由于WF做為NET3.0的一部份發部,
這樣使我們在設計WF應用時有更多的方案:
常用的有
1.使用服務器單一引擎處理所有客戶端流程
2.每個客戶端啟動自己的業引擎,服務器從事數據保存與消息中轉的工作
這些具體的應用,其他文章已經描述過,這里就不多說了;
本例的應用場景是第二種,
當然也可以很容易的改成第一種方式,數據交換用WebService,技巧就用[由一個WF項目說起]中我貼出的那段代碼為基礎。
要實現該例,要在數據庫中建一些表,每張表代表一個具體的業務表單,表的個數、名稱、結構任意,例子就是要動態掛單。
掛單類代碼
using?System.Collections.Generic;
using?System.Text;
namespace?WFDataForm
{
????class?DynamicDataForm?:?System.Windows.Forms.Form
????{
????????public?string?conString?=?"";
????????public?string?tabName;
????????public?DynamicDataForm(string?db,?string?tabName)
????????{
????????????this.conString?=?string.Format("data?source=.;initial?catalog={0};integrated?security=true",?db);
????????????this.tabName?=?tabName;
????????????InitializeComponent();
????????????OperateData();
????????????GetTabStruct();
????????????ShowTabStruct(tabRow);?;
????????}
????????private?System.Windows.Forms.Panel?dataPanel;
????????private?System.Windows.Forms.Button?okButton;
????????private?System.Data.SqlClient.SqlConnection?con;
????????private?System.Data.SqlClient.SqlCommand?cmd_Select;
????????private?System.Data.SqlClient.SqlCommand?cmd_Insert;
????????System.Collections.Generic.List<TabField>?tabRow?=?new?List<TabField>();
????????private?void?InitializeComponent()
????????{
????????????okButton?=?new?System.Windows.Forms.Button();
????????????okButton.Text?=?"完成";
????????????okButton.Click?+=?new?EventHandler(okButton_Click);
????????????dataPanel?=?new?System.Windows.Forms.Panel();
????????????dataPanel.AutoScroll?=?true;
????????????dataPanel.BorderStyle?=?System.Windows.Forms.BorderStyle.FixedSingle;
????????????dataPanel.Width?=?400;
????????????dataPanel.Height?=?200;
????????????dataPanel.Top?=?30;
????????????this.Width?=?410;
????????????this.Controls.Add(okButton);
????????????this.Controls.Add(dataPanel);
????????}
????????void?okButton_Click(object?sender,?EventArgs?e)
????????{
????????????con.Open();
????????????string?s?=?GetInsetrString(tabRow);
????????????System.Console.WriteLine(s);
????????????cmd_Insert.CommandText?=?s;
????????????cmd_Insert.ExecuteNonQuery();
????????????con.Close();
????????????this.Close();
????????}
????????private?void?OperateData()
????????{
????????????con?=?new?System.Data.SqlClient.SqlConnection(this.conString);
????????????cmd_Select?=?con.CreateCommand();
????????????cmd_Select.CommandText?=?string.Format("select?*?from?{0}?where?1>?2",?this.tabName);
????????????cmd_Insert?=?con.CreateCommand();
????????}
????????private?void?GetTabStruct()
????????{
????????????con.Open();
????????????System.Data.SqlClient.SqlDataReader?rd?=?cmd_Select.ExecuteReader();
????????????for?(int?i?=?0;?i?<?rd.FieldCount;?i++)
????????????{
????????????????TabField?f?=?new?TabField();
????????????????f.fieldType?=?rd.GetFieldType(i);
????????????????f.fieldName?=?rd.GetName(i);
????????????????tabRow.Add(f);
????????????}
????????????rd.Close();
????????????con.Close();
????????}
????????private?void?ShowTabStruct(List<TabField>?o)
????????{
????????????int?x?=?120;
????????????int?y?=?30;
????????????int?i?=?0;
????????????foreach?(TabField?temp?in?o)
????????????{
????????????????System.Windows.Forms.Label?l?=?new?System.Windows.Forms.Label();
????????????????l.Text?=?temp.fieldName;
????????????????l.Top?=?y?*?i;
????????????????System.Windows.Forms.TextBox?t?=?new?System.Windows.Forms.TextBox();
????????????????t.Left?=?x;
????????????????t.Top?=?y?*?i;
????????????????t.Tag?=?temp.fieldType;
????????????????i?=?i?+?1;
????????????????this.dataPanel.Controls.Add(l);
????????????????this.dataPanel.Controls.Add(t);
????????????}
????????}
????????private?void?GetData(List<TabField>?o)
????????{
????????????int?i?=?0;
????????????foreach?(System.Windows.Forms.Control?temp?in?this.dataPanel.Controls)
????????????{
????????????????if?(temp?is?System.Windows.Forms.TextBox)
????????????????{
????????????????????tabRow[i].fieldValue?=?temp.Text;
????????????????????i?=?i?+?1;
????????????????}
????????????}
????????}
????????private?string?GetInsetrString(List<TabField>?o)
????????{
????????????GetData(o);
????????????string?f?=?"";
????????????string?v?=?"";
????????????foreach?(TabField?temp?in?o)
????????????{
????????????????f?=?f?+?temp.fieldName?+?",";
????????????????if?(temp.fieldType.ToString()?==?"System.Int32")
????????????????{
????????????????????v?=?v?+?temp.fieldValue?+?",";
????????????????}
????????????????else
????????????????{
????????????????????v?=?v?+?"'"?+?temp.fieldValue?+?"'"?+?",";
????????????????}
????????????}
????????????f?=?DeleteRearwardSymbol(f);
????????????v?=?DeleteRearwardSymbol(v);
????????????string?s?=?string.Format("insert?into?{0}?({1})?values?({2})",?tabName,?f,?v);
????????????return?s;
????????}
????????private?string?DeleteRearwardSymbol(string?s)
????????{
????????????string?temp?=?s.Remove(s.Length?-?1);
????????????return?temp;
????????}
????????class?TabField
????????{
????????????public?System.Type?fieldType?=?null;
????????????public?string?fieldName?=?"";
????????????public?object?fieldValue?=?null;
????????}
????}
}
工作流代碼
?
namespace?WFDataForm{
????public?sealed?partial?class?Workflow1:?SequentialWorkflowActivity
????{
????????public?Workflow1()
????????{
????????????InitializeComponent();
????????
????????}
????????private?string[]?tab;
????????private?string?tabs="";
????????public?string?Tabs
????????{
????????????get?{?return?tabs;?}
????????????set?{?tabs?=?value;?}
????????}
????????private?bool?whileTag=false;
????????private?int?sp?=?0;
????????private?void?codeActivity1_ExecuteCode(object?sender,?EventArgs?e)
????????{
????????????if?(sp?<?tab.Length)
????????????{
????????????????new?DynamicDataForm("a",tab[sp]).ShowDialog();
????????????????sp?=?sp?+?1;
????????????}
????????????else
????????????{
????????????????whileTag?=?true;
????????????????
????????????}
????????}
????????private?void?codeActivity2_ExecuteCode(object?sender,?EventArgs?e)
????????{
????????????tab?=?tabs.Split(new?char[]?{?','?});
????????}
?????????
????}
}
引擎代碼
????????System.Workflow.Runtime.WorkflowInstance?instance;
????????private?void?button1_Click(object?sender,?EventArgs?e)
????????{
????????????Dictionary<string,?object>?parameters?=?new?Dictionary<string,?object>();
????????????parameters.Add("Tabs",textBox1.Text);
???????????
????????????instance?=?wr.CreateWorkflow(typeof(WFDataForm.Workflow1),?parameters);
????????????instance.Start();
????????????
????????}
????????private?void?Form1_Load(object?sender,?EventArgs?e)
????????{
????????????wr?=?new?System.Workflow.Runtime.WorkflowRuntime();
????????????wr.StartRuntime();
????????????wr.WorkflowCompleted?+=?new?EventHandler<System.Workflow.Runtime.WorkflowCompletedEventArgs>(wr_WorkflowCompleted);
????????}
????????void?wr_WorkflowCompleted(object?sender,?System.Workflow.Runtime.WorkflowCompletedEventArgs?e)
????????{
????????????MessageBox.Show("完成");
????????
????????}
????????private?void?Form1_FormClosing(object?sender,?FormClosingEventArgs?e)
????????{
????????????wr.StopRuntime();
????????}
????????private?void?button2_Click(object?sender,?EventArgs?e)
????????{
??????????
????????????instance.Resume();
????????}
運行說明
在文本框中輸入表明,用[,]分開,每多一個表,流程就會多一個邏輯結點
注意修改一下連接字串,以指定數據庫
數據類型驗證沒有寫,但類型分析已寫完,只接加上就可以了
更多的擴展應用,在后面的文章中會展開了談,本文全當熱身
本例來自于[資料(2).rar]內容
本例代碼http://files.cnblogs.com/foundation/WFDataForm.rar
轉載于:https://www.cnblogs.com/foundation/archive/2007/05/12/743887.html
總結
- 上一篇: SQL2005删除用户的时候,产生“数据
- 下一篇: 什么是k-NN算法?怎样实现?终于有人讲