Flex與.NET WebService(續:與DataTable協作)

來源:互聯網
上載者:User

  .NET WebService經常會返回DataSet資料,因此,如果Flex能夠處理DataSet就完美了。本以為這是很簡單的事,但實際試過後,才發現有問題。難道Flex不能與.NET的DataSet一起工作嗎?
經過上網尋找,沒找到解決方案。發現有的人是用結構數組來返回資料,有的是轉換成xml字串來返回資料。不論是什麼方法,都要對現有的.NET WebService進行大的變動,並且也不方便。

最後發現,使用DataTable返回資料就可以直接與Flex協作。

舉一個很簡單的例子:
現有如下的資料表:

WebMethod為GetCustomer:

[WebService(Namespace = "http://tempuri.org/")]
public class Service : System.Web.Services.WebService
{
    public Service () {    }

    [WebMethod]
    public DataTable GetCustomers() {
        DataSet ds = new DataSet();
        using (SqlConnection conn = new SqlConnection(@"Data Source=muf\sqlexpress;Initial Catalog=Test;Integrated Security=True"))
        using (SqlDataAdapter ada = new SqlDataAdapter("select * from customer", conn))
        {
            ada.Fill(ds, "customer");     
        }
        return ds.Tables["customer"];
    }
    
}

現在Flex只要很簡單的增加<mx:WebService>就可以了:<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="Service.GetCustomers.send();">
    <mx:WebService id="Service" 
        wsdl="http://localhost:1708/WebSite1/Service.asmx?WSDL"         
        useProxy="false">
        <mx:operation name="GetCustomers">
            <mx:request>                
            </mx:request>
        </mx:operation>

    </mx:WebService>

    <mx:DataGrid x="25" y="10" dataProvider="{Service.GetCustomers.lastResult.diffgram.NewDataSet.customer}">
        <mx:columns>
            <mx:DataGridColumn headerText="ID" dataField="ID"/>
            <mx:DataGridColumn headerText="Name" dataField="Name"/>
            <mx:DataGridColumn headerText="Address" dataField="Address"/>
            <mx:DataGridColumn headerText="Age" dataField="Age"/>
            <mx:DataGridColumn headerText="Gender" dataField="Gender"/>
        </mx:columns>
    </mx:DataGrid>
    
</mx:Application>

運行結果如下:

這裡頭主要注意的是,DataProvider的綁定值為:Service.GetCustomers.lastResult.diffgram.NewDataSet.customer。
其中,NewDataSet是DataSet的名稱,customer是表的名稱。 

思考:為什麼DataSet不行而DataTable行呢?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.