flex调用webservice中的datatable结果写入datagrid

webservice配置文件

 

<appSettings>
		<add key="sqlConDuke" value="server=10.9.34.88;database=PanoSalado;uid=sa;pwd=123"/>
	</appSettings>

Webservice代码

 

 

 //测试数据库连接
    [WebMethod(Description = "!!!测试数据库连接,链接字符串在web.config中")]
    public bool conectDB()
    {
        strCon = ConfigurationSettings.AppSettings["sqlConDuke"];
        conn = new SqlConnection(strCon);
        try
        {
            conn.Open();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
        finally
        {
            conn.Close();
        }
    }

    [WebMethod(Description = "!!!获得所有数据(DataTable),strTb,eg:PanView")]
    public DataTable getAllDT(string strTb)
    {
        bool ifCon = conectDB();
        if (ifCon)
        {
            string sql_BfCar = "SELECT * FROM " + strTb;
            da = new SqlDataAdapter(sql_BfCar, conn);
            myds = new DataSet();
            da.Fill(myds);
            mydt=myds.Tables[0];
            return mydt;
        }
        else
        {
            return null;
        }
    }

 

Flex代码:

 

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
	<fx:Script>
		<![CDATA[ 
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;

			[Bindable]
			public var arr:ArrayCollection=new ArrayCollection();
			
			protected function button1_clickHandler(event:MouseEvent):void
			{
				myWeb.getAllDT("PanView"); //分别为webservice中的方法和参数
			}
			internal function onSuccess(evt:ResultEvent):void
			{
				 arr=this.myWeb.getAllDT.lastResult.Tables.Table.Rows; //Table为webservice中的datatable名字
				 Alert.show(arr.length.toString());
			}

			protected function button2_clickHandler(event:MouseEvent):void
			{
				myWeb.getLinkDTByImgName("PanView","20131019072740-0000150");
			}
			internal function onSuccess1(evt:ResultEvent):void
			{
				arr=this.myWeb.getLinkDTByImgName.lastResult.Tables.Table.Rows;
				Alert.show(arr.length.toString());
			}
			
			internal function onFault(evt:FaultEvent):void
			{
				Alert.show("调用WebService方法失败,详细:" + evt.fault.faultDetail.toString());
			}

		]]>
	</fx:Script>
	<s:layout>
		<s:BasicLayout/>
	</s:layout>
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
		<s:WebService id="myWeb" wsdl="http://10.19.1.55/serverPano/WebServiceDuke.asmx?WSDL" fault="onFault(event)">
			<s:operation name="getAllDT" result="onSuccess(event)"></s:operation>
			<s:operation name="getLinkDTByImgName" result="onSuccess1(event)"></s:operation>
		</s:WebService>
	</fx:Declarations>
	<mx:DataGrid x="121" y="177" width="737" height="402" id="dg1" dataProvider="{arr}"> 
		<mx:columns>
			<mx:DataGridColumn headerText="ID" dataField="ID"/> <!--webservice中datatable中的字段-->
			<mx:DataGridColumn headerText="ImageName" dataField="ImageName"/>
			<mx:DataGridColumn headerText="LinkGuid" dataField="LinkGuid"/>
		</mx:columns>
	</mx:DataGrid>
	<s:Button x="254" y="82" label="返回所有" click="button1_clickHandler(event)"/>
	<s:Button x="500" y="82" label="按照片名查询" click="button2_clickHandler(event)"/>
</s:Application>



 











 

posted on   love so much  阅读(359)  评论(0编辑  收藏  举报

努力加载评论中...
< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

导航

点击右上角即可分享
微信分享提示