Flex读写数据
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="init()"> <fx:Script> <![CDATA[ import flash.net.URLRequest; import flash.net.URLLoader; import flash.events.Event; import flash.text.engine.EastAsianJustifier; import flash.events.MouseEvent; import flash.net.URLVariables; private function init():void { login_btn.addEventListener(MouseEvent.CLICK,loginHandler); } function loginHandler(e:MouseEvent):void { // variables用来收集flash端的数据,其中variables点后面可以是任何的字符串。 //服务器端使用Request接受数据时跟的是 ariables点后面的字符串。 var variables:URLVariables = new URLVariables(); variables.names = username_txt.text; //服务器地址http://10.1.1.70:8002/Default.aspx var url:String = "http://10.1.1.70:8002/responseFlex.ashx"; var request:URLRequest = new URLRequest(encodeURI(url)); request.data = variables; //mess.text=variables+"---"+encodeURI(url); request.method=URLRequestMethod.POST; var loader:URLLoader = new URLLoader(); loader.load(request); loader.addEventListener(Event.COMPLETE,completeHandler); } function completeHandler(e:Event) { detial_txt.text = e.target.data; } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <mx:TextArea x="10" y="10" height="30" width="50" id="username_txt"/> <mx:TextArea x="70" y="10" height="30" width="500" id="mess"/> <mx:Button id="login_btn" x="10" y="50" label="search" /> <mx:TextArea x="10" y="100" height="100" width="500" id="detial_txt"/> </s:Application>
http://10.1.1.70:8002是我自己部署的网站地址,c#代码如下:
using System.Web; using System.Web.Services; using System.Data.SqlClient; namespace FlexReadSql { /// <summary> /// $codebehindclassname$ 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class ResponseFlex : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request["names"] != null) { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = "Data Source=ZGY-WUHAN;Initial Catalog=assql-test;Persist Security Info=True;User ID=sa;Password=********"; conn.Open(); SqlCommand com = new SqlCommand(); com.Connection = conn; com.CommandText = "select * from use_db where names=@name"; SqlParameter para = new SqlParameter("@name", context.Request["names"]); com.Parameters.Add(para); using (SqlDataReader dr = com.ExecuteReader()) { if (dr.Read()) { //context.Response.ContentType = "text/plain"; context.Response.Write(dr["pass"].ToString()); context.Response.End(); } else context.Response.Write("error!!!"); } } } else context.Response.Write("没有参数!"); } public bool IsReusable { get { return false; } } } }