Flex 学习随笔 ---- 使用WebService 与数据库连接
任何一個網絡工具, 如果不能和數據打交道,那它就是失敗的。
還好Flex是可以的,由于本人剛學,就用asp.net+c#來講下這個簡單的連接。
Flex 和數據庫通訊現在只能使用Service,如httpservice,rpcservice,webservice等等。
如果還有其他的,抱歉,我還沒學。。。
現在開始:
首先建個 WebService.
?
Code
using?System;
using?System.Web;
using?System.Collections;
using?System.Web.Services;
using?System.Web.Services.Protocols;
using?System.Configuration;
using?System.Data;
using?System.Data.SqlClient;
using?System.Xml;
///?<summary>
///?GetXMLDataService?的摘要說明
///?</summary>
[WebService(Namespace?=?"http://tempuri.org/")]
[WebServiceBinding(ConformsTo?=?WsiProfiles.BasicProfile1_1)]
public?class?GetXMLDataService?:?System.Web.Services.WebService?{
????public?GetXMLDataService?()?{
????????//如果使用設計的組件,請取消注釋以下行?
????????//InitializeComponent();?
????}
????[WebMethod]
????public?string?GetXMLData(string?sqlCommand)?{
????????string?sqlConnectionString?=?ConfigurationManager.ConnectionStrings["testconn"].ToString();
????????
????????DataSet?ds?=?new?DataSet();
????????ds?=?SqlHelper.ExecuteDataset(sqlConnectionString,?CommandType.Text,?sqlCommand);??????
????????string?xmlStr?=?"<?xml?version=\"1.0\"?encoding=\"utf-8\"?>?\n";?
????????return?xmlStr+ds.GetXml();
????}
???????
}
?
這個Web服務有個方法,訪問數據庫,并返回一個標準的xml字符串。
注意? string xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?> \n";
如果不加這條,那在Flex中不能格式化成xmllist。
現在來看Flex的。
?
Code<mx:WebService?id="GetXMLDataService"?
????????????wsdl="http://localhost:4512/Flex/GetXMLDataService.asmx?wsdl"?
????????????showBusyCursor="true"?useProxy="false">
????????<mx:operation?name="GetXMLData"?result="formatResult();">
????????????<mx:request>
????????????????<sqlCommand>
????????????????????<!--?{sqlCommand}?-->
????????????????????{txtSqlCommand.text}
????????????????</sqlCommand>
????????????</mx:request>
????????</mx:operation>????????
????</mx:WebService>
上面代碼是表示如何使用 這個Web服務。http://localhost:4512/Flex/GetXMLDataService.asmx?wsdl?這個是我web服務運行的本機地址。
這個是獲取數據并格式化數據
?
Code//?格式化通過WebService獲取的數據??????Type:?XMLList
????????????private?function?formatResult():void{
????????????????xmlData?=?new?XML(GetXMLDataService.GetXMLData.lastResult);
????????????????this.columnchart1.dataProvider?=?xmlData.children();
????????????????BindData();
????????????}
其中 BindData() 是自己寫的綁定控件數據方法。可以刪除。
?
測試運行, 首先運行 web服務, 并保證 http://localhost:4512/Flex/GetXMLDataService.asmx?wsdl?能夠訪問。
這個是測試我是綁定一個ColumnChart。圖片就懶放上來了。 抓圖累。。。。。
?
?
轉載于:https://www.cnblogs.com/yalon/archive/2008/12/06/Flex_2.html
總結
以上是生活随笔為你收集整理的Flex 学习随笔 ---- 使用WebService 与数据库连接的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PowerShell远程管理Window
- 下一篇: MySQL入门-3:安装与客户端工具