Flex通过Web Service操作MySQL
项目中遇到的问题一步一步解决,也慢慢总结这过程中的点点滴滴学习笔记,今天把整个串起来,算是一个完整的项目解决方案。其实还有很多问题需要在项目中解决,比如camera capture,最后生产pdf格式的数据分析结果文件。
一些准备知识可以参看以下两个链接:
一:ASP.NET连接MySQL数据库,可以参考http://blog.csdn.net/li_007/archive/2008/12/10/3486614.aspx
二:Web Service操作数据库,可以参考http://blog.csdn.net/li_007/archive/2008/12/15/3525627.aspx
好了,可以先看看效果,截图如下:
图一 customer表
图二 Web Service页面
图三 执行GetCustomerInfo得到的数据
图四 Flex中DataGrid显示绑定的数据
首先,在Web Service中GetCustomerInfo函数的实现,是一个查询所有,然后返回XML格式的string
[WebMethod] public string GetCustomerInfo() { MySqlConnection conn = new MySqlConnection("server=localhost;user id=guest;password=xxx;database=new_bmw"); string query = "select * from customer"; MySqlCommand command = new MySqlCommand(query, conn); conn.Open(); MySqlDataAdapter da = new MySqlDataAdapter(); da.SelectCommand = command; DataSet ds = new DataSet(); da.Fill(ds); string str = ds.GetXml(); //注意这里将查询结果转化成XML格式的string conn.Close(); return str; }
同样也先把Flex实现代码贴出来,再解释。
<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" showStatusBar = "false" applicationComplete="InitApplication()" backgroundImage="@Embed(source='../../Images/background.jpg')"> <mx:Script> <!--[CDATA[ import mx.rpc.events.ResultEvent; // //AS code // import flash.display.StageDisplayState; import flash.events.MouseEvent; import mx.controls.Alert; [Bindable] private var xmlCustomerInfo:XML; //function private function InitApplication():void { this.stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE; wsCustomerInfo.GetCustomerInfo.send(); } private function DataProcessing(Args:ResultEvent):void { this.xmlCustomerInfo = new XML(Args.result); trace(xmlCustomerInfo.Table.Name); } ]]--> </mx:Script> <mx:TabNavigator x="175" y="257" width="750" height="499" backgroundAlpha="0.2" id="MainTab" tabHeight="35" borderStyle="none" borderThickness="50" visible="true" fontSize="16"> <mx:Canvas label="客户身份查询" width="100%" height="100%" id="tabQuery" fontSize="16" focusEnabled="true" autoLayout="true"> <mx:DataGrid width="710" height="344" id="dgCustomerInfo" backgroundAlpha="0.4" selectable="true" textAlign="center" dataProvider="{xmlCustomerInfo.Table}" x="20" doubleClickEnabled="true" allowMultipleSelection="true" y="93"> <mx:columns> <mx:DataGridColumn headerText="编号" dataField="CustomerID" width="60"/> <mx:DataGridColumn headerText="姓名" dataField="Name"/> <mx:DataGridColumn headerText="性别" dataField="Gender" width="50"/> <mx:DataGridColumn headerText="地址" dataField="Address"/> <mx:DataGridColumn headerText="电话" dataField="Telephone" width="60"/> <mx:DataGridColumn headerText="学历" dataField="Education"/> <mx:DataGridColumn headerText="爱好" dataField="Hobby"/> <mx:DataGridColumn headerText="工作" dataField="Occupation"/> </mx:columns> </mx:DataGrid> </mx:Canvas> <mx:Form label="客户信息录入" width="100%" height="100%" id="tabInfoRecord"> </mx:Form> <mx:Form label="客户身份验证" width="100%" height="100%" id="tabCheckIn"> </mx:Form> <mx:Form label="客户成绩录入" width="100%" height="100%" id="tabGradeRecord"> </mx:Form> </mx:TabNavigator> <mx:WebService id="wsCustomerInfo" wsdl="http://localhost/luxwebservice/Service.asmx?WSDL" fault="Alert.show(event.fault.faultString, 'Error')"> <mx:operation name="GetCustomerInfo" resultFormat="object" result="DataProcessing(event);"> </mx:operation> </mx:WebService> </mx:WindowedApplication>
结合上面的代码,首先先看mx:WebService组件,设置组件id,关键是设置wsdl,应该是你webService页面的url加“?WSDL",然后是mx:operation组件,注意name属性值一定要与WebService服务函数名一样,resultFormat从字面意思就知道是设置返回结果的类型,我们返回的是XML格式的一个string,所以是object,不是xml,最后我加了个result事件,左右待会解释。
DataGrid的设置见代码,由于想要绑定获取数据,而我们获取的是一个内容为xml格式的string,这是没法绑定的,所以就在mx:operation组件中添加了对result事件的相应,在这个事件函数中将string转化成xml对象,这样才可以很好地与DataGrid的columns来绑定。为了转化,首先Bindable了一个XML对象。
[Bindable] private var xmlCustomerInfo:XML; //然后在DataProcessing监听器中实现string转化成xml, private function DataProcessing(Args:ResultEvent):void { this.xmlCustomerInfo = new XML(Args.result); trace(xmlCustomerInfo.Table.Name); } //最后DataGrid的绑定见 dataProvider="{xmlCustomerInfo.Table}"//注意语法,每个columns的绑定是xml操作问题,自己学习xml
好了,设置,显示等等都做好了,现在需要解决的问题是怎么样去调用访问Web Service,mx:webservice组件方法如下:
wsCustomerInfo.GetCustomerInfo.send();//注意语法
好了,整个过程就是这样的,关键绑定过程不要出错,不然得到了数据显示不出来。