#眉標=Office開發技術 #大標=InfoPath Project Toolkit for VS.NET #副標=搞定Office 2003開發技術 (1) #作者=文/朱明中 ============================ 程式1 以外部資料來源填入資料至表單的範例程式碼片段 [InfoPathEventHandler (MatchPath="/mstns:CustomerOrders/mstns:CustomerOrder/mstns:Customer/mstns:CustomerName", EventType=InfoPathEventType.OnAfterChange)] public void CustomerName_OnAfterChange(DataDOMEvent e) { // connect to external data source to fill form control’s data // without using InfoPath Secondary Data Source. SqlConnection conn = new SqlConnection("initial catalog=northwind; user id=sa; password="); SqlCommand cmd = new SqlCommand(); SqlDataReader dr = null; cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT * FROM Customers WHERE CustomerID='" + e.NewValue.ToString() + "'"; conn.Open(); dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); if (e.IsUndoRedo) { // An undo or redo operation has occurred and the DOM is read-only. return; } // A field change has occurred and the DOM is writable. Write code here to respond // to the changes. // write items. if (!dr.HasRows) { dr.Close(); return; } dr.Read(); this.thisXDocument.DOM.selectSingleNode("//mstns:Customer/mstns:Contact").text = dr.GetString(dr.GetOrdinal("ContactName")); this.thisXDocument.DOM.selectSingleNode("//mstns:Customer/mstns:BillingAddress").text = dr.GetString(dr.GetOrdinal("Address")); this.thisXDocument.DOM.selectSingleNode("//mstns:Customer/mstns:ShippingAddress").text = dr.GetString(dr.GetOrdinal("Address")); this.thisXDocument.DOM.selectSingleNode("//mstns:Customer/mstns:Telephone").text = dr.GetString(dr.GetOrdinal("Phone")); this.thisXDocument.DOM.selectSingleNode("//mstns:Customer/mstns:Fax").text = dr.GetString(dr.GetOrdinal("Fax")); dr.Close(); conn.Dispose(); cmd.Dispose(); dr = null; } ========================