Flex調用WebService訪問MSSQL數據庫
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12">
<mx:Script>
<![CDATA[
import mx.events.ResizeEvent;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
protected function init():void{
Alert.show("阿拉山口的附件","提示");
// ws.loadWSDL();
}
private function onResult(evt:ResultEvent):void
{
// var msg:String=evt.target.name;
Alert.show(evt.result.toString());
}
internal function onResultP(evt:ResultEvent):void{
Grid.dataProvider=WS.getInfo.lastResult.Tables.Table.Rows;
}
internal function onResult2(evt:ResultEvent):void{
myGrid.dataProvider = WS.GetList.lastResult;
}
internal function onResultPL(evt:ResultEvent):void{
Grid.dataProvider=WS.GetPList.lastResult;
}
]]>
</mx:Script>
<mx:Button x="50" y="51" label="單擊我" fontSize="12" click="init()"/>
<mx:Button x="26" y="209" label="獲取webservice的信息" fontSize="12" click="WS.HelloWorld.send()"/>
<mx:Button x="129" y="291" label="返回DataTable" width="118" click="WS.getInfo.send()"/>
<mx:Button x="129" y="337" label="返回List" width="118" click="WS.GetPList.send()"/>
<mx:Button label="返回List(泛型)" click="WS.GetList.send();" x="131" y="457"></mx:Button>
//wsdl為引用的webservice的地址
<mx:WebService id="WS" wsdl="http://localhost:5800/WebSite5/Service.asmx?wsdl" fault="Alert.show(event.fault.faultString,'Error')" showBusyCursor="true">
//此處的name="HelloWorld" 為WebService中的方法名 下面的getInfo等都為webservice中的方法
<mx:operation name="HelloWorld" resultFormat="object" result="onResult(event);">
<mx:request>
//此處的<name>表示為HelloWorld方法中的參數
<name>{txt.text}</name>
<age>45</age>
</mx:request>
</mx:operation>
<mx:operation name="getInfo" resultFormat="object" result="onResultP(event)"/>
<mx:operation name="GetPList" resultFormat="object" result="onResultPL(event)"/>
<mx:operation name="GetList" result="onResult2(event);"/>
</mx:WebService>
<mx:TextInput id="txt" x="26" y="144" text="小明"/>
//綁定數據
<mx:DataGrid id="Grid" x="309.5" y="290" width="302" height="107">
<mx:columns>
<mx:DataGridColumn headerText="姓名" dataField="name"/>
<mx:DataGridColumn headerText="性別" dataField="sex"/>
<mx:DataGridColumn headerText="年齡" dataField="age"/>
</mx:columns>
</mx:DataGrid>
<mx:DataGrid id="myGrid" x="309.5" y="456">
<mx:columns>
<mx:DataGridColumn headerText="ID" dataField="ID"/>
<mx:DataGridColumn headerText="歌曲名稱" dataField="Song"/>
<mx:DataGridColumn headerText="歌手" dataField="Artist"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>

浙公網安備 33010602011771號