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>