flex3通過fluorinefx跟asp.net進行資料互動

來源:互聯網
上載者:User
這幾天不太忙把原來的一些小的樣本整理一下,這個樣本是一個簡單的通過fluorinefx與asp.net結合來時間flex串連資料庫的.因為一直以來flex+java大家都認為是最好的搭檔但是asp.net也是很好的選擇,個人認為asp.net的效率要比java的高.好了現在進入正題.

      flex部分代碼如下:

mxml檔案
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute" creationComplete="fs.GetData();" fontSize="13"><mx:Script><![CDATA[import mx.collections.ArrayCollection;import mx.rpc.events.ResultEvent;import mx.controls.Alert;//獲得資料後的處理函數public function GetDataHandle(e:ResultEvent):void{//獲得的資料繫結至DataGrid組件dg.dataProvider=e.result.tables.serverInfo.initialData as Array;}//插入資料成功後的處理函數public function InsertDataHandle(e:ResultEvent):void{//提示插入成功Alert.show("插入資料成功");//重新擷取資料fs.GetData();}]]></mx:Script><mx:Panel width="474" height="489" title="通知"><mx:DataGrid id="dg"><mx:columns><mx:DataGridColumn headerText="編號" dataField="0"/><mx:DataGridColumn headerText="標題" dataField="1"/><mx:DataGridColumn headerText="內容" dataField="2"/><mx:DataGridColumn headerText="發行者" dataField="3"/></mx:columns></mx:DataGrid><mx:Canvas width="395" height="243"><mx:Label x="37" y="27" text="標題"/><mx:TextInput x="80" y="25" id="txtTitle"/><mx:Label x="37" y="155" text="發行者"/><mx:TextInput x="80" y="153" id="txtPublisher"/><mx:Label x="37" y="53" text="內容"/><mx:TextArea x="80" y="55" width="278" height="90" id="txtContent"/><mx:Button x="121" y="198" label="添加" id="btnInsert" click="fs.InsertData(txtTitle.text,txtContent.text,txtPublisher.text);"/></mx:Canvas></mx:Panel><mx:RemoteObjectdestination="fluorine"id="fs"source="remoting.GetSQLServer2000Data"showBusyCursor="true"><mx:methodname="GetData"result="GetDataHandle(event)"/><mx:methodname="InsertData"result="InsertDataHandle(event)"/></mx:RemoteObject></mx:Application>
flex裡的services_config.xml這個檔案是一個設定檔就好比asp.net裡的webconfig意思差不多吧都是xml結構,這個檔案要跟mxml檔案放在一個路徑下檔案內容如下:
<?xml version="1.0" encoding="UTF-8"?><services-config><services><service id="remoting-service"class="flex.messaging.services.RemotingService"messageTypes="flex.messaging.messages.RemotingMessage"><destination id="fluorine"><channels><channel ref="my-amf"/></channels><properties><source>*</source></properties></destination></service></services><channels><channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel"><endpoint uri="http://localhost:6199/WebSite2/Gateway.aspx" class="flex.messaging.endpoints.AMFEndpoint"/></channel-definition></channels></services-config>
好了到現在為止flex部分的檔案已經完全展現給大家了.
 
下面是asp.net部分
 
首先確定在機器裡已經安裝了fluorinefx,現在fluorinefx已經可以支援在vs2008中使用了
開啟vs2008或vs2005以後在就會出現一個fluorinefx的選項,建立好以後就可以通過修改cs代碼來實現串連資料庫的功能
代碼如下:
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.SqlClient;//引用“System.Data.SqlClient”using System.Collections;//引用“System.Collections”using FluorineFx.Management.Web;using FluorineFx;/// <summary>/// GetSQLServer2000Data 的摘要說明/// </summary>namespace remoting //名稱空間,可自訂{[RemotingService()]public class GetSQLServer2000Data{public GetSQLServer2000Data(){}public DataSet GetData()//獲得資料庫資料{SqlConnection conn = new SqlConnection();//定義“SqlConnnection”類執行個體//資料庫連接字串conn.ConnectionString = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=sa";//定義“SqlCommand”執行個體,從“Notes”表中取資料SqlCommand command = new SqlCommand("select * from Notes", conn);conn.Open();//開啟串連SqlDataAdapter da = new SqlDataAdapter();//定義“SqlDataAdapter”類執行個體da.SelectCommand = command;//將“command”值傳遞給“SqlDataAdapter”的“SelectCommand”屬性DataSet ds = new DataSet();//定義“DataSet”類執行個體da.Fill(ds, "tables");//取資料//關閉資料庫conn.Close();return ds;}public void InsertData(string title, string content, string publisher)//插入資料{SqlConnection conn = new SqlConnection();//定義“SqlConnnection”類執行個體//資料庫連接字串conn.ConnectionString = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=sa";SqlCommand command = new SqlCommand("select Max(noteId) from Notes", conn);conn.Open();//開啟串連SqlDataAdapter da = new SqlDataAdapter();//定義“SqlDataAdapter”類執行個體da.SelectCommand = command;//將“command”值傳遞給“SqlDataAdapter”的“SelectCommand”屬性DataSet ds = new DataSet();//定義“DataSet”類執行個體da.Fill(ds, "tables");//取資料string newid = (Convert.ToInt32(ds.Tables["tables"].Rows[0][0].ToString()) + 1).ToString();command = new SqlCommand("insert into Notes values('" + newid + "','" + title.Trim() + "','" + content.Trim() + "','" + publisher.Trim() + "')", conn);command.ExecuteNonQuery();conn.Close();}}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.