These days, I am not very busy sorting out some of the original small examples. This example is a simple time to connect to the database using fluorinefx and Asp.net. since Flex + Java has always been regarded as the best partner, but Asp.net is also a good choice. I personally think Asp.net is more efficient than Java. now let's get started.
Flex SectionCodeAs follows:
Mxml File
<? 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; // The processing function public function getdatahandle (E: resultevent) after obtaining data: void {// bind the obtained data to the DataGrid component DG. dataprovider = E. result. tables. serverinfo. initialdata as array;} // public function insertdatahandle (E: resultevent) after successful data insertion: void {// prompt that alert is successfully inserted. show ("data inserted successfully"); // obtain data again FS. getdata () ;}]]> </MX: SCRIPT> <mx: panel width = "474" Height = "489" Title = "notification"> <mx: dataGrid id = "DG"> <mx: columns> <mx: datagridcolumn headertext = "no." datafield = "0"/> <mx: datagridcolumn headertext = "title" datafield = "1"/> <mx: datagridcolumn headertext = "content" datafield = "2"/> <mx: datagridcolumn headertext = "publisher" datafield = "3"/> </MX: columns> </MX: DataGrid> <mx: canvas width = "395" Height = "243"> <mx: Label x = "37" Y = "27" text = "title"/> <mx: textinput x = "80" Y = "25" id = "txttitle"/> <mx: label x = "37" Y = "155" text = "publisher"/> <mx: textinput x = "80" Y = "153" id = "txtpublisher"/> <mx: label x = "37" Y = "53" text = "content"/> <mx: textarea x = "80" Y = "55" width = "278" Height = "90" id = "txtcontent"/> <mx: button x = "121" Y = "198" label = "add" 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>
The services_config.xml file in Flex is a configuration file, which is similar to the webconfig file in Asp.net. It is in an XML structure. The content of this file must be put in a path with the mxml file as follows:
<? 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>
Now, some flex files have been fully presented to you.
Below is the Asp.net Section
First, make sure that fluorinefx has been installed on the machine. fluorinefx can now be used in vs2008.
After vs2008 or vs2005 is enabled, a fluorinefx option will appear. After establishment, you can modify the CS code to connect to the database.
The Code is as follows:
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; // reference "system. data. sqlclient "using system. collections; // reference "system. collections "using fluorinefx. management. web; using fluorinefx; // <summary> /// Summary of getsqlserver2000data /// </Summary> namespace remoting // namespace, you can customize {[remotingservice ()] public class getsqlserver2000data {public getsqlserver2000data () {} public dataset getdata () // obtain database data {sqlconnection conn = new sqlconnection (); // define the "sqlconnnection" class instance // database connection string Conn. connectionstring = "Data Source = .; initial catalog = school; persist Security info = true; user id = sa; Password = sa "; // define the" sqlcommand "instance, obtain data from the "Notes" Table sqlcommand command = new sqlcommand ("select * From Notes", Conn); Conn. open (); // open the connection sqldataadapter da = new sqldataadapter (); // defines the "sqldataadapter" class instance da. selectcommand = command; // pass the "command" value to the "selectcommand" attribute dataset DS = new dataset () of "sqldataadapter"; // define the "dataset" class instance da. fill (DS, "tables"); // retrieves data // closes the database Conn. close (); Return Ds;} public void insertdata (String title, string content, string publisher) // insert data {sqlconnection conn = new sqlconnection (); // define the "sqlconnnection" class instance // database connection string 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 (); // open the connection sqldataadapter da = new sqldataadapter (); // defines the "sqldataadapter" class instance da. selectcommand = command; // pass the "command" value to the "selectcommand" attribute dataset DS = new dataset () of "sqldataadapter"; // define the "dataset" class instance da. fill (DS, "tables"); // obtain the data 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 ();}}}