C# (ASMX) WebService 定義自訂回傳類別 ReturnResult,包含索引與 DataTable。Flex 透過 `
[Serializable]
public class ReturnResult {
public int curIndex { get; set; }
public DataTable idxList { get; set; }
}
[WebMethod(Description = "依照片名取得 Link 所有資料")]
public ReturnResult getLinkDTByImgName(string strTb, string strImgName) {
ReturnResult rr = new ReturnResult();
if (conectDB()) {
// 先查單筆取得 curIndex 與 LinkGuid
DataTable dt = getInfoByImgName(strTb, strImgName);
rr.curIndex = Convert.ToInt32(dt.Rows[0][0]);
string linkGuid = dt.Rows[0][1].ToString();
// 再查該 LinkGuid 所有資料
string sql = "SELECT * FROM dbo." + strTb + " WHERE LinkGuid LIKE '%" + linkGuid + "%'";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
rr.idxList = ds.Tables[0];
}
return rr;
}
// 輔助方法
public DataTable getInfoByImgName(string strTb, string strImgName) { ... }
public bool conectDB() { ... }
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="https://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="initApp(event)">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
[Bindable] private var arr:ArrayCollection = new ArrayCollection();
private var idx: String = "";
protected function initApp(event:FlexEvent):void {
myWeb.getLinkDTByImgName("PanView", "20131019072740-0000150");
}
protected function onResult(event:ResultEvent):void {
// event.result 對應 ReturnResult 實例
idx = event.result.curIndex.toString();
// DataTable 序列化為巢狀物件:Tables.Table.Rows
arr = new ArrayCollection(event.result.idxList.Tables.Table.Rows);
Alert.show("索引: " + idx + " 筆數: " + arr.length);
}
protected function onFault(event:FaultEvent):void {
Alert.show("WebService 錯誤: " + event.fault.toString());
}
]]>
</fx:Script>
<fx:Declarations>
<s:WebService id="myWeb"
wsdl="https://10.19.1.55/serverPano/WebServiceDuke.asmx?WSDL"
fault="onFault(event)">
<s:operation name="getLinkDTByImgName"
result="onResult(event)"/>
</s:WebService>
</fx:Declarations>
</s:Application>
| C# 型別 | WSDL / Flex 型別 | 存取路徑 |
|---|---|---|
| int curIndex | xsd:int | event.result.curIndex |
| DataTable idxList | 複雜型別 (DataSet 衍生) | event.result.idxList.Tables.Table.Rows (ArrayCollection) |
Flex 透過 `