flex3通过fluorinefx跟asp.net进行数据交互

      flex专区 2008-7-5 17:46

       这几天不太忙把原来的一些小的示例整理一下,这个示例是一个简单的通过fluorinefx与asp.net结合来时间flex连接数据库的.因为一直以来flex+java大家都认为是最好的搭档但是asp.net也是很好的选择,个人认为asp.net的效率要比java的高.好了现在进入正题.

      flex部分代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"    
  4.   
  5.   
  6.   
  7.     layout="absolute" creationComplete="fs.GetData();" fontSize="13">  
  8.   
  9.     <mx:Script>  
  10.   
  11.         <![CDATA[  
  12.  
  13.             import mx.collections.ArrayCollection;  
  14.  
  15.             import mx.rpc.events.ResultEvent;  
  16.  
  17.             import mx.controls.Alert;  
  18.  
  19.             //获得数据后的处理函数  
  20.  
  21.             public function GetDataHandle(e:ResultEvent):void  
  22.  
  23.             {  
  24.  
  25.                 //获得的数据绑定至DataGrid组件  
  26.  
  27.                 dg.dataProvider=e.result.tables.serverInfo.initialData as Array;  
  28.  
  29.             }  
  30.  
  31.             //插入数据成功后的处理函数  
  32.  
  33.             public function InsertDataHandle(e:ResultEvent):void  
  34.  
  35.             {  
  36.  
  37.                 //提示插入成功  
  38.  
  39.                 Alert.show("插入数据成功");  
  40.  
  41.                 //重新获取数据  
  42.  
  43.                 fs.GetData();  
  44.  
  45.             }  
  46.  
  47.         ]]>  
  48.   
  49.     </mx:Script>  
  50.   
  51.     <mx:Panel width="474" height="489" title="通知">  
  52.   
  53.         <mx:DataGrid id="dg">  
  54.   
  55.             <mx:columns>  
  56.   
  57.                 <mx:DataGridColumn headerText="编号" dataField="0"/>  
  58.   
  59.                 <mx:DataGridColumn headerText="标题" dataField="1"/>  
  60.   
  61.                 <mx:DataGridColumn headerText="内容" dataField="2"/>  
  62.   
  63.                 <mx:DataGridColumn headerText="发布者" dataField="3"/>  
  64.   
  65.             </mx:columns>  
  66.   
  67.         </mx:DataGrid>  
  68.   
  69.         <mx:Canvas width="395" height="243">  
  70.   
  71.             <mx:Label x="37" y="27" text="标题"/>  
  72.   
  73.             <mx:TextInput x="80" y="25" id="txtTitle"/>  
  74.   
  75.             <mx:Label x="37" y="155" text="发布者"/>  
  76.   
  77.             <mx:TextInput x="80" y="153" id="txtPublisher"/>  
  78.   
  79.             <mx:Label x="37" y="53" text="内容"/>  
  80.   
  81.             <mx:TextArea x="80" y="55" width="278" height="90" id="txtContent"/>  
  82.   
  83.             <mx:Button x="121" y="198" label="添加" id="btnInsert" click="fs.InsertData(txtTitle.text,txtContent.text,txtPublisher.text);"/>  
  84.   
  85.         </mx:Canvas>  
  86.   
  87.     </mx:Panel>  
  88.   
  89.   
  90.   
  91.     <mx:RemoteObject     
  92.   
  93.         destination="fluorine"  
  94.   
  95.         id="fs"  
  96.   
  97.         source="remoting.GetSQLServer2000Data"  
  98.   
  99.         showBusyCursor="true"  
  100.   
  101.         >  
  102.   
  103.        
  104.   
  105.             <mx:method  
  106.   
  107.                 name="GetData"  
  108.   
  109.                 result="GetDataHandle(event)"  
  110.   
  111.             />      
  112.   
  113.             <mx:method  
  114.   
  115.                 name="InsertData"  
  116.   
  117.                 result="InsertDataHandle(event)"  
  118.   
  119.             />                  
  120.   
  121.                
  122.   
  123.     </mx:RemoteObject>  
  124.   
  125. </mx:Application>  
  1. flex里的services_config.xml这个文件是一个配置文件就好比asp.net里的webconfig意思差不多吧都是xml结构,这个文件要跟mxml文件放在一个路径下文件内容如下:  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <services-config>  
  4.   
  5.     <services>    
  6.   
  7.         <service id="remoting-service"  
  8.   
  9.                  class="flex.messaging.services.RemotingService"  
  10.   
  11.                  messageTypes="flex.messaging.messages.RemotingMessage">  
  12.   
  13.             <destination id="fluorine">  
  14.   
  15.                 <channels>    
  16.   
  17.                     <channel ref="my-amf"/>  
  18.   
  19.                 </channels>    
  20.   
  21.                 <properties>    
  22.   
  23.                     <source>*</source>    
  24.   
  25.                 </properties>    
  26.   
  27.             </destination>    
  28.   
  29.         </service>    
  30.   
  31.     </services>    
  32.   
  33.     <channels>    
  34.   
  35.         <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">  
  36.   
  37.             <endpoint uri="http://localhost:6199/WebSite2/Gateway.aspx" class="flex.messaging.endpoints.AMFEndpoint"/>  
  38.   
  39.         </channel-definition>  
  40.   
  41.     </channels>  
  42.   
  43. </services-config>  
  1. 好了到现在为止flex部分的文件已经完全展现给大家了.  
  1. 下面是asp.net部分  
  1. 首先确定在机器里已经安装了fluorinefx,现在fluorinefx已经可以支持在vs2008中使用了  
  1. 打开vs2008或vs2005以后在就会出现一个fluorinefx的选项,建立好以后就可以通过修改cs代码来实现连接数据库的功能  
  1. <PRE class=csharp name="code">using System;   
  2.   
  3. using System.Data;   
  4.   
  5. using System.Configuration;   
  6.   
  7. using System.Web;   
  8.   
  9. using System.Web.Security;   
  10.   
  11. using System.Web.UI;   
  12.   
  13. using System.Web.UI.WebControls;   
  14.   
  15. using System.Web.UI.WebControls.WebParts;   
  16.   
  17. using System.Web.UI.HtmlControls;   
  18.   
  19. using System.Data.SqlClient;//引用“System.Data.SqlClient”   
  20.   
  21. using System.Collections;//引用“System.Collections”   
  22.   
  23. using FluorineFx.Management.Web;   
  24.   
  25. using FluorineFx;   
  26.   
  27.   
  28.   
  29.   
  30.   
  31. /// <summary>  
  32.   
  33. /// GetSQLServer2000Data 的摘要说明   
  34.   
  35. /// </summary>  
  36.   
  37.   
  38.   
  39. namespace remoting //名称空间,可自定义   
  40.   
  41. {   
  42.   
  43.     [RemotingService()]   
  44.   
  45.     public class GetSQLServer2000Data   
  46.   
  47.     {   
  48.   
  49.         public GetSQLServer2000Data()   
  50.   
  51.         {   
  52.   
  53.   
  54.   
  55.         }   
  56.   
  57.         public DataSet GetData()//获得数据库数据   
  58.   
  59.         {   
  60.   
  61.             SqlConnection conn = new SqlConnection();//定义“SqlConnnection”类实例   
  62.   
  63.             //数据库连接字符串   
  64.   
  65.             conn.ConnectionString = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=sa";   
  66.   
  67.             //定义“SqlCommand”实例,从“Notes”表中取数据   
  68.   
  69.             SqlCommand command = new SqlCommand("select * from Notes", conn);   
  70.   
  71.             conn.Open();//打开连接   
  72.   
  73.             SqlDataAdapter da = new SqlDataAdapter();//定义“SqlDataAdapter”类实例   
  74.   
  75.             da.SelectCommand = command;//将“command”值传递给“SqlDataAdapter”的“SelectCommand”属性   
  76.   
  77.             DataSet ds = new DataSet();//定义“DataSet”类实例   
  78.   
  79.             da.Fill(ds, "tables");//取数据   
  80.   
  81.             //关闭数据库   
  82.   
  83.             conn.Close();   
  84.   
  85.             return ds;   
  86.   
  87.         }   
  88.   
  89.         public void InsertData(string title, string content, string publisher)//插入数据   
  90.   
  91.         {   
  92.   
  93.             SqlConnection conn = new SqlConnection();//定义“SqlConnnection”类实例   
  94.   
  95.             //数据库连接字符串   
  96.   
  97.             conn.ConnectionString = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=sa";   
  98.   
  99.             SqlCommand command = new SqlCommand("select Max(noteId) from Notes", conn);   
  100.   
  101.             conn.Open();//打开连接   
  102.   
  103.             SqlDataAdapter da = new SqlDataAdapter();//定义“SqlDataAdapter”类实例   
  104.   
  105.             da.SelectCommand = command;//将“command”值传递给“SqlDataAdapter”的“SelectCommand”属性   
  106.   
  107.             DataSet ds = new DataSet();//定义“DataSet”类实例   
  108.   
  109.             da.Fill(ds, "tables");//取数据   
  110.   
  111.             string newid = (Convert.ToInt32(ds.Tables["tables"].Rows[0][0].ToString()) + 1).ToString();   
  112.   
  113.             command = new SqlCommand("insert into Notes values('" + newid + "','" + title.Trim() + "','" + content.Trim() + "','" + publisher.Trim() + "')", conn);   
  114.   
  115.             command.ExecuteNonQuery();   
  116.   
  117.             conn.Close();   
  118.   
  119.   
  120.   
  121.         }   
  122.   
  123.     }   
  124.   
  125. }</PRE>  
  126.   
  127. 好了这段代码就是给大家一个抛砖引玉的作用.如果还有不明白的可以找我要代码.我的群62197976在群共享里有代码  
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}