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:RemoteObject destination="fluorine" id="fs" source="remoting.GetSQLServer2000Data" showBusyCursor="true" > <mx:method name="GetData" result="GetDataHandle(event)" /> <mx:method name="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(); } } }